以下回傳所有arrProps具有匹配arrValues值的內容:
const arr = [
{ arrProps: [{name: '1', prop2: 'aaa'}], arrValues: ['apple', 'orange'] },
{ arrProps: [{name: '2', prop2: 'bbb'}], arrValues: ['taco', 'orange' ] },
{ arrProps: [{ name: '3', prop2: 'ccc' }], arrValues: ['fish', 'apple', 'orange'] }
];
const result = arr
.flatMap(({ arrValues }) => arrValues ) // all values
.filter((value, index, coll) => coll.indexOf(value) === index) //unique values
.reduce((acc, value) => {
const parentProp = arr // name, prop2, arrValues
.filter((obj) => obj.arrValues.includes(value))
.map((obj) => obj.arrProps[0].name);
//.map((obj) => [{ source: obj.arrProps[0].name, value: obj.arrValues[0], sourceID: null}]);
acc[value] = (acc[value] ? [...acc[value], parentProp] : [parentProp])
.join(',');
acc[value] = parentProp
return acc;
}, {})
console.log(result);
console.table(result);

我需要向 arrValues 添加其他屬性,從而使用物件而不是值:
const arr = [
{ arrProps: [{ name: '1', prop2: 'aaa' }], arrValues: [{ symbol: 'apple', id: '1.apple' }, {symbol: 'orange', id: '1.orange'}] },
{ arrProps: [{ name: '2', prop2: 'bbb' }], arrValues: [{ symbol: 'fish', id: '2.fish' }, { symbol: 'pizza', id: '2.pizza' }, { symbol: 'red', id: '2.red' }] },
{ arrProps: [{ name: '3', prop2: 'ccc' }], arrValues: [{ symbol: 'grape', id: '3.grape'}, { symbol: 'apple', id: '3.apple' }] },
];
從 simple 'apple',我需要添加一些額外的唯一標識值,即{ symbol: 'apple', id: '1.apple' }
我確定它在這里:
aMap.filter((value, index, coll) => coll.indexOf(value) === index) // <--- filter issue
哪個回傳物件(以前的資料模型,只是值 'apple', 'orange'...):
Array(7) [Object, Object, Object, Object, Object, Object, Object]
[[Prototype]]: Array(0)
length: 7
0: Object {symbol: "apple", id: "1.apple"}
1: Object {symbol: "orange", id: "1.orange"}
2: Object {symbol: "fish", id: "2.fish"}
3: Object {symbol: "pizza", id: "2.pizza"}
4: Object {symbol: "red", id: "2.red"}
5: Object {symbol: "grape", id: "3.grape"}
6: Object {symbol: "apple", id: "3.apple"}
__proto__: Array(0)
我有很多時間試圖過濾arrValues[0].symbol. 然后我需要附加arrValues[0].id到結果。我認為使用 arrProps 創建一個新物件并且唯一的 ID 會是最好的。
所以在 的情況下apple,第一列應該呈現{parent: arrProps[0], id: arrValues[0].id},等等。
- - 更新 - -
Rather than just showing the name of arrPorps[0].name, I need an object returned, as per the last line above (a change from the original question).
I had thought about finding some way to append arrProps to the output but got stuck there. Since I can hydrate the data model, I added some additional identifying properties to arrValues[] (wait, does this and arrProps[] really need to be an array, it's just an object - refactor?):
const arr = [
{ arrProps: [{ name: '1', prop2: 'aaa' }], arrValues: [{ exchange: '1', symbol: 'apple', id: '1.apple' }, {exchange: '1', symbol: 'orange', id: '1.orange' }] },
{ arrProps: [{ name: '2', prop2: 'bbb' }], arrValues: [{ exchange: '2', symbol: 'fish', id: '2.fish' }, { exchange: '2', symbol: 'pizza', id: '2.pizza' }, { exchange: '2', symbol: 'red', id: '2.red' }] },
{ arrProps: [{ name: '3', prop2: 'ccc' }], arrValues: [{ exchange: '3', symbol: 'grape', id: '3.grape' }, { exchange: '3', symbol: 'apple', id: '3.apple' }] },
];
Then changed the output, creating an output object,
From: .map(({ arrProps }) => arrProps[0].name)
To: .map(({ arrValues }) => [{ exchange: arrValues[0].exchange, symbol: arrValues[0].symbol, id: arrValues[0].id }]);
Whole updated code:
const arr = [
{ arrProps: [{ name: '1', prop2: 'aaa' }], arrValues: [{ exchange: '1', symbol: 'apple', id: '1.apple' }, {exchange: '1', symbol: 'orange', id: '1.orange' }] },
{ arrProps: [{ name: '2', prop2: 'bbb' }], arrValues: [{ exchange: '2', symbol: 'fish', id: '2.fish' }, { exchange: '2', symbol: 'pizza', id: '2.pizza' }, { exchange: '2', symbol: 'red', id: '2.red' }] },
{ arrProps: [{ name: '3', prop2: 'ccc' }], arrValues: [{ exchange: '3', symbol: 'grape', id: '3.grape' }, { exchange: '3', symbol: 'apple', id: '3.apple' }] },
];
const objectValues = arr.flatMap(({ arrValues }) => arrValues); // not necessary if we refactor, removing these arrays?
const values = objectValues.map(({ symbol }) => symbol);
const uniqueValues = [...new Set(values)];
const result = uniqueValues.reduce((acc, value) => {
acc[value] = arr
.filter(({ arrValues }) => {
const symbols = arrValues.map(({ symbol }) => symbol)
return symbols.includes(value);
})
//.map(({ arrProps }) => arrProps[0].name)
.map(({ arrValues }) => [{ exchange: arrValues[0].exchange, symbol: arrValues[0].symbol, id: arrValues[0].id }]);
//.join(',');
return acc;
}, {});
console.log("RAW:" JSON.stringify(result));
console.log(result);
console.table(result);
Object.entries(result).forEach(([k, v]) => {
console.log(` ----] The value '${k}' exists in:`);
console.table(JSON.stringify(v));
//console.log("The pair: ", k)
////console.log("The value: ", v)
v.forEach(_pool => console.log(`
Exchange: ${_pool[0].exchange}
symbol: ${_pool[0].symbol}
ID: ${_pool[0].id}
`));
})
Two problems:
- I think the arrays are not necessary in the data model
- The output data is wrong.
{ "apple":[ [ { "exchange":"1", "symbol":"apple", "id":"1.apple" } ], [ { "exchange":"3", "symbol":"grape", // should be "apple" "id":"3.grape" // should be "3.apple" } ] ], "orange":[ [ { "exchange":"1", "symbol":"apple", // should be "orange" "id":"1.apple" // should be "1.orange" } ] ], "fish":[ [ { "exchange":"2", "symbol":"fish", "id":"2.fish" } ] ], "pizza":[ [ { "exchange":"2", "symbol":"fish", // should be pizza "id":"2.fish" // should be 2.pizza } ] ], "red":[ [ { "exchange":"2", "symbol":"fish", // should be "red" "id":"2.fish" // should be "2.red" } ] ], "grape":[ [ { "exchange":"3", "symbol":"grape", "id":"3.grape" } ] ] }
--- UPDATE 2 ---
Going back to my suspicion of the data being wrong, well, it appears to be.
而不是根據其相關的常用詞命名每個物件:apple, orange, fish,這需要引數化并洗掉多余的陣列(偽編碼物件):
[{
itemName: 'apple',
itemParam2: 'xxx',
itemPools: [
{ source: '3', symbol: 'apple', id: '3.apple', cost: '0.00' },
{ source: '4', symbol: 'apple', id: '4.apple', cost: '0.00' },
{ source: '4', symbol: 'apple', id: '4b.apple', cost: '0.00' }
],
itemName: 'orange',
itemParam2: 'yyy',
itemPools: [
...
itemName: 'fish',
itemParam2: 'zzz',
itemPools: [
...
}]
僅供參考:需要驗證結果資料物件的格式是否正確
實際上,orange并且fish應該從模型中被遺漏的作為itemPools.length不> 1。
所以除了更新的模型屬性外,只itemPool.length > 1需要添加專案。
沒有這個,我無法通過它的屬性找到一個陣列項。
在這里給出一些未來的背景。
uj5u.com熱心網友回復:
經過幾種方法,我改變了解決方案。在我看來,它現在更簡單、更通用。
const arr = [
{ arrProps: [{ name: '1', prop2: 'aaa' }], arrValues: [{ symbol: 'apple', id: '1.apple' }, {symbol: 'orange', id: '1.orange'}] },
{ arrProps: [{ name: '2', prop2: 'bbb' }], arrValues: [{ symbol: 'fish', id: '2.fish' }, { symbol: 'pizza', id: '2.pizza' }, { symbol: 'red', id: '2.red' }] },
{ arrProps: [{ name: '3', prop2: 'ccc' }], arrValues: [{ symbol: 'grape', id: '3.grape'}, { symbol: 'apple', id: '3.apple' }] },
];
const result = arr.reduce((acc, { arrProps: [{ name }], arrValues }) => {
arrValues.map(({ symbol }) => symbol)
.forEach((value) => {
acc[value] = acc[value] ? `${acc[value]},${name}`: name;
});
return acc;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果我們考慮使用過濾器的先前方法,則解決方案可能如下:
const arr = [
{ arrProps: [{ name: '1', prop2: 'aaa' }], arrValues: [{ symbol: 'apple', id: '1.apple' }, {symbol: 'orange', id: '1.orange'}] },
{ arrProps: [{ name: '2', prop2: 'bbb' }], arrValues: [{ symbol: 'fish', id: '2.fish' }, { symbol: 'pizza', id: '2.pizza' }, { symbol: 'red', id: '2.red' }] },
{ arrProps: [{ name: '3', prop2: 'ccc' }], arrValues: [{ symbol: 'grape', id: '3.grape'}, { symbol: 'apple', id: '3.apple' }] },
];
const objectValues = arr.flatMap(({ arrValues }) => arrValues);
const values = objectValues.map(({ symbol }) => symbol);
const uniqueValues = [...new Set(values)];
const result = uniqueValues.reduce((acc, value) => {
acc[value] = arr
.filter(({ arrValues }) => {
const symbols = arrValues.map(({ symbol }) => symbol)
return symbols.includes(value);
})
.map(({ arrProps }) => arrProps[0].name)
.join(',');
return acc;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
- -更新 - -
const arr = [
{ arrProps: [{ name: '1', prop2: 'aaa' }], arrValues: [{ exchange: '1', symbol: 'apple', id: '1.apple' }, {exchange: '1', symbol: 'orange', id: '1.orange' }] },
{ arrProps: [{ name: '2', prop2: 'bbb' }], arrValues: [{ exchange: '2', symbol: 'fish', id: '2.fish' }, { exchange: '2', symbol: 'pizza', id: '2.pizza' }, { exchange: '2', symbol: 'red', id: '2.red' }] },
{ arrProps: [{ name: '3', prop2: 'ccc' }], arrValues: [{ exchange: '3', symbol: 'grape', id: '3.grape' }, { exchange: '3', symbol: 'apple', id: '3.apple' }] },
];
const result = arr.reduce((acc, { arrValues }) => {
arrValues.forEach((value) => {
acc[value.symbol] = acc[value.symbol]
? [...acc[value.symbol], value]
: [value];
});
return acc;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
---更新3---
const arr = [
{ arrProps: [{ name: '1', prop2: 'aaa' }], arrValues: [{ exchange: '1', symbol: 'apple', id: '1.apple' }, {exchange: '1', symbol: 'orange', id: '1.orange' }] },
{ arrProps: [{ name: '2', prop2: 'bbb' }], arrValues: [{ exchange: '2', symbol: 'fish', id: '2.fish' }, { exchange: '2', symbol: 'pizza', id: '2.pizza' }, { exchange: '2', symbol: 'red', id: '2.red' }] },
{ arrProps: [{ name: '3', prop2: 'ccc' }], arrValues: [{ exchange: '3', symbol: 'grape', id: '3.grape' }, { exchange: '3', symbol: 'apple', id: '3.apple' }] },
];
const reduceArray = (data, poolsCountEdge = 1) => {
const result = data.reduce((acc, { arrValues }) => {
arrValues.forEach((value) => {
const item = acc.find(({ name }) => name === value.symbol);
if (item) {
item.pools.push(value);
item.poolsCount = item.pools.length;
} else {
acc.push({ name: value.symbol, poolsCount: 1, pools: [value] });
}
});
return acc;
}, []);
return result.filter(({ poolsCount }) => poolsCount > poolsCountEdge)
};
console.log(reduceArray(arr));
.as-console-wrapper { max-height: 100% !important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395710.html
標籤:javascript 数组 排序 筛选 过滤
上一篇:按特定狀態對專案串列進行排序
