我試圖操作這個物件,使它與它的id的對應計數相匹配。
const data= [
[
{id: "333"/span>, count: 555}。
{id: "2", count: 133}。
{id: "444", count: 13}。
{id: "433", count: 32}。
{id: "3333", count: 2}.
],
[
{id: "333", count: 5565}。
{id: "2", count: 1633}。
{id: "444", count: 136}。
{id: "433", count: 362}。
{id: "3333", count: 62}。
],
[
{id: "333", count: 585}。
{id: "2", count: 1383}。
{id: "444", count: 138}。
{id: "433", count: 328}。
{id: "3333", count: 82}。
],
]
對此:
const newData = [
{ id: "333", count: [555, 5565, 585] },
{ id: "2", count: [133, 1633, 1383] },
{ id: "444", count: [13, 136, 138] },
{ id: "433", count: [32, 362, 328] },
{ id: "3333", count: [2, 62, 82] }
]
我試過這樣的方法,但這只是給我一個陣列,每個計數都是獨立的。
let ob = test.reduce( ( acc, e, i) => /span> {
let obj = {}。
e.forEach((value, index) => {
obj[value.recommendationItemIdExternal] = [value.recommendationItemRating] 。
})
acc.push(obj)。
return acc.
}, [] );
uj5u.com熱心網友回復:
一個嵌套的for回圈就可以了。只需遍歷資料并將計數(或b)值存盤在另一個物件中,使用id作為鍵。
let adjusted = {};
data.forEach(e => {
for (let item of e) {
if (adjusted[item.id] == undefined)
adjusted[item.id] =[]。
if(item.count != undefined)
adjusted[item.id].push(item.count)。
else if(item.b != undefined)
adjusted[item.id].push(item.b)。
}
});
console.log(調整后)。
。
const data = [
[{
id: "333"。
count: 555。
},
{
id: "2",
count: 133。
},
{
id: "444",
count: 13。
},
{
id: "433",
count: 32。
},
{
id: "3333",
count: 2。
},
],
[{
id: "333",
count: 5565。
},
{
id: "2"/span>,
count: 1633。
},
{
id: "444",
count: 136。
},
{
id: "433",
count: 362。
},
{
id: "3333",
count: 62.
},
],
[{
id: "333",
b: 585。
},
{
id: "2",
b: 1383。
},
{
id: "444"/span>,
b: 138。
},
{
id: "433",
b: 328。
},
{
id: "3333",
b: 82。
},
],
]
let adjusted = {};
data.forEach(e => {
for (let item of e) {
if (adjusted[item.id] == undefined)
adjusted[item.id] =[]。
if(item.count != undefined)
adjusted[item.id].push(item.count)。
else if(item.b != undefined)
adjusted[item.id].push(item.b)。
}
});
console.log(adjusted);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
。const data = [
[
{ id: "333"/span>, count: 555 },
{ id: "2", count: 133 },
{ id: "444", count: 13 },
{ id: "433", count: 32 },
{ id: "3333", count: 2 },
],
[
{ id: "333", count: 5565 },
{ id: "2", count: 1633 },
{ id: "444", count: 136 },
{ id: "433", count: 362 },
{ id: "3333", count: 62 },
],
[
{ id: "333", count: 585 },
{ id: "2", count: 1383 },
{ id: "444", count: 138 },
{ id: "433", count: 328 },
{ id: "3333", count: 82 },
],
];
const map = new Map() 。
newData = [];
const dict = data.flat().reduce((acc, curr) => /span> {
!acc.has(curr.id)
? map.set(curr.id, [curr.count])
: map.get(corr.id).push(curr.count)。
return acc;
}, map)。
for (let [id, count] of dict) {
newData.push({ id, count })。
}
console.log(newData);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你可以使用.reduce()和.map()陣列方法的組合,如下面的演示。
const newArray = data. reduce((acc, cur) => cur。 map((o, i) =>/span> ({
id: o.id,
count: [...(acc[i] && acc[i].count || []), o.count] 。
})), []);
請注意這只在原始陣列的每個元素的ids順序相同的情況下有效,否則需要更多的處理。
DEMO
。const data= [
[
{id: "333"/span>, count: 555}。
{id: "2", count: 133}。
{id: "444", count: 13}。
{id: "433", count: 32}。
{id: "3333", count: 2}.
],
[
{id: "333", count: 5565}。
{id: "2", count: 1633}。
{id: "444", count: 136}。
{id: "433", count: 362}。
{id: "3333", count: 62}。
],
[
{id: "333", count: 585}。
{id: "2", count: 1383}。
{id: "444", count: 138}。
{id: "433", count: 328}。
{id: "3333", count: 82}。
],
];
const newArray = data.reduce((acc,cur) => cur. map((o,i) => ({id:o。 id,count:[...(acc[i] && acc[i].count || []),o.count]), [] )。)
console.log( newArray );
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/312773.html
標籤:
上一篇:我怎樣才能把一個有效的日期作為一個字串,但如果不保持默認建構式分配的默認值?
下一篇:動態分割陣列元素
