我有一個這樣的JS物件陣列:
我有一個這樣的JS物件陣列。
var myArray = [
{ line: 20, text: [31, 80] },
{ line: 10, text: [80, 22] }
]
行在整個myArray中是唯一的,每一行都有一些文本(不唯一)。 如何將每個文本匹配到其對應的行中?
最后的結果應該是這樣的:
var myNewArray = [
{ text: 31, line: [20] },
{ text: 80, line: [20, 10] },
{ text: 22, line: [10] }
uj5u.com熱心網友回復:
一些使用Map的方法。
結果你得到一個臨時地圖,它收集了所有的文本,按行分組。為了得到一個物件的陣列,將鍵/值對映射為eanted屬性。
由于有資料的嵌套陣列,你需要eiter將資料規范化以獲得單一的
line/text值,然后按text添加分組,
。<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>const data = [{ line: 20, text: [31, 80] }, { line: 10, text: [80, 22] }] 。 result = Array.from( 讀取資料 .flatMap(({ line, text }) => text. map(text => ({ text, line })) .reduce((m, { text, line }) => m.set(text, [. ...(m.get(text) || []), line]), new Map)。) ([text, line]) =>({ text, line }) ); console.log(result);或者在一個步驟中進行,但采用嵌套的方式減少外部(
。line)和內部陣列(text陣列)。<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>const 資料 = [ { line: 20, text: [31, 80] }, { line: 10, text: [80, 22] }. ], result = Array.from( data.reduce( (m, { line, text }) => text.reduce( (n, text) => n.set(text, [...(n.get(text) || [] ), line]) 。) m ), new Map, m) ), ([text, line]) =>({ text, line }) ); console.log(result);
uj5u.com熱心網友回復:
下面是如何做的:
。var myArray = [
{ line: 20, text: [31, 80] },
{ line: 10, text: [80, 22] }
]
var newArray = myArray.reduce((acc, {line, text}) => //span> {
for( let t of text ) {
const match = acc. find(({text}) => text == t) // check if the text already exists in newArray
if( match ) match.lines.push(line) //如果存在,將該行添加到該文本。
else acc.push({text:t, lines:[line]}) // it not, create a new object with that line.
}
return acc
}, [])
console.log( newArray )
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
或者首先生成一個Object而不是陣列,如果你的資料集巨大,這樣會更快,然后在最后將其轉換為陣列:
。var myArray = [
{ line: 20, text: [31, 80] },
{ line: 10, text: [80, 22] }
]
//為文本/行生成一個鍵/值對。
var newArray = myArray.reduce((acc, {line, text}) => {
for( let t of text )
acc[t] = [...(acc[t] || []), line] 。
return acc
}, {})
//將上述物件轉換為物件陣列(AKA Collection)。
newArray = Object.entries(newArray)。 map(([text,lines]) =>({text, lines}))
console.log( newArray )
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
可能最簡單的是首先建立一個中間的Map,按文本索引行:
const data = [
{line: 20, text: [31,80]},
{line: 10, text: [80,22]}.
];
const result = [...data.reduce((map, {line, text}) => /span> {
text.forEach(t => {
map.has(t) || map.set(t, [] )。
map.get(t).push(line)。
});
return map。
}, new Map()).entries()】。] map(([text, line]) =>({text, line}))。
console.log(result);
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
這里有一個簡單的解決方案,可以解決你的問題。
var myArray = [
{ line: 20, text: [31, 80] },
{ line: 10, text: [80, 22] }
]
var myNewArray = [] 。
myArray.forEach((item) =>/span> {
item.text.forEach((t) => {
const index = myNewArray.findIndex((e => e.text ==t)
index === -1 ?
? myNewArray.push({text: t, line: [item.line]})
: myNewArray[index].line.push( item.line)
})
})
console.log(myNewArray)
<iframe name="sif6" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/328361.html
標籤:
