我有以下物件陣列:
[
{
"id": 1,
"price": 22,
"from": "00:00:00",
"to": "02:00:00"
},
{
"id": 1,
"price": 23,
"from": "02:00:00",
"to": "04:00:00"
},
{
"id": 2,
"price": 10,
"from": "04:00:00",
"to": "1.00:00:00"
}
]
我需要對其進行重組,因此它按 ID 組合物件,并創建一個新的嵌套物件陣列:
[
{
"id": 1,
"prices": [
{
"price": 22,
"from": "00:00:00",
"to": "02:00:00"
},
{
"price": 23,
"from": "02:00:00",
"to": "04:00:00"
},
]
}
{
"id": 2,
"prices": [
{
"price": 10,
"from": "04:00:00",
"to": "1.00:00:00"
}
]
}
]
有沒有一種簡單的方法可以做到這一點?我迷失在 split、forEach 和 maps 中。謝謝。
uj5u.com熱心網友回復:
一種方法是使用 aMap來收集每個 id 的價格。首先為每個 id 關聯一個空的價格串列,然后迭代資料以填充這些陣列,最后提取Map值以獲得結果:
const data = [{"id": 1,"price": 22,"from": "00:00:00","to": "02:00:00"},{"id": 1,"price": 23,"from": "02:00:00","to": "04:00:00"},{"id": 2,"price": 10,"from": "04:00:00","to": "1.00:00:00"}];
const map = new Map(data.map(({id}) => [id, { id, prices: [] }]));
for (const {id, ...rest} of data) map.get(id).prices.push(rest);
const result = [...map.values()];
console.log(result);
uj5u.com熱心網友回復:
您可以使用Array.reduce方法和Array.findIndex來轉換您的資料。
const data = [{
"id": 1,
"price": 22,
"from": "00:00:00",
"to": "02:00:00"
},
{
"id": 1,
"price": 23,
"from": "02:00:00",
"to": "04:00:00"
},
{
"id": 2,
"price": 10,
"from": "04:00:00",
"to": "1.00:00:00"
}
];
const transformedData = data.reduce((acc, item) => {
const priceData = {
price: item.price,
to: item.to,
from: item.from,
};
const index = acc.findIndex(({
id
}) => id === item.id);
if (index === -1) {
return [
...acc,
{
id: item.id,
prices: [
priceData
]
},
];
} else {
acc[index].prices.push(priceData);
return acc;
}
}, []);
console.log(transformedData);
uj5u.com熱心網友回復:
const items = [
{
"id": 1,
"price": 22,
"from": "00:00:00",
"to": "02:00:00"
},
{
"id": 1,
"price": 23,
"from": "02:00:00",
"to": "04:00:00"
},
{
"id": 2,
"price": 10,
"from": "04:00:00",
"to": "1.00:00:00"
}
]
const result = items
.map(i => i.id)
.filter((item, pos, self) => self.indexOf(item) == pos)
.map( i => ({
id : i,
prices : items
.filter(item => item.id === i)
.map(({ price, from, to}) => ({price, from , to}) )
}) )
console.log(result)
uj5u.com熱心網友回復:
是的,有一個簡單的方法使用reduce().
const input = [
{
id: 1,
price: 22,
from: "00:00:00",
to: "02:00:00",
},
{
id: 1,
price: 23,
from: "02:00:00",
to: "04:00:00",
},
{
id: 2,
price: 10,
from: "04:00:00",
to: "1.00:00:00",
},
];
const output = input.reduce((nested, cur) => {
const objWithoutId = (({id, ...o}) => o)(cur);
if (!nested[cur.id]) {
nested[cur.id] = {
id: cur.id,
prices: [objWithoutId]
};
}
else nested[cur.id].prices.push(objWithoutId);
return nested;
}, {});
console.log(Object.values(output));
.as-console-wrapper { max-height: 100% !important; top: 0; }
解釋
我們使用 JavaScript 物件回圈輸入。對于每個物件,我們檢查它的 ID。當我們的物件中還沒有存盤該 ID 時,我們創建一個新物件,其中包含id和一個prices陣列以及當前值(沒有id屬性)。如果我們已經遇到了同樣的情況,id我們只需要將當前值(沒有id屬性)推送到已經存在的陣列中。
由于我們只有一個回圈,并且查找需要O(1)時間,因此該演算法需要花費一些時間O(n)才能給出有效的結果。
使用這個單線
const objWithoutId = (({id, ...o}) => o)(cur);
我們創建一個新物件,其中包含除id.
最后但并非最不重要的是,我們只需要使用Object.values(). 除了 JavaScript 物件,您還可以使用 aMap來執行具有相同運行時屬性的等效演算法。
uj5u.com熱心網友回復:
您可以使用Object.entries,Array#reduce和Array#map方法,如下所示:
const input = [ { "id": 1, "price": 22, "from": "00:00:00", "to": "02:00:00" }, { "id": 1, "price": 23, "from": "02:00:00", "to": "04:00:00" }, { "id": 2, "price": 10, "from": "04:00:00", "to": "1.00:00:00" } ],
output = Object.entries(input.reduce(
(acc, {id,...rest}) =>
({...acc, [id]: [...(acc[id] || []), rest]}), {}
))
.map(([id, prices]) => ({id,prices}));
console.log( output );
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/465557.html
標籤:javascript 数组 目的
