[英]How to split an object within an array into multiple objects?
我有一個對象數組,就像名稱一樣,fixedValue 是固定的,並且類別和總數在內部 fixedValue 數組的每個 object 中也是固定的。 但是“Col 2”、“Col 3”等可以是隨機值,並且可以有多個。 因此,除 category 或 total 之外的所有其他鍵都可以命名為“FERFVCEEF erfe”或任何隨機詞:
originalDataSet = [
{
"fixedValue": [
{
"category": "SD",
"total": 2,
"Col 2": 208,
"Col 3": 88,
"Col 4": 4,
....
},
{
"category": "EFG",
"total": 1,
"Col 2": 106,
"Col 3": 46,
"Col 4": 4,
....
},
{
"category": "ERG",
"total": 1,
"Col 2": 107,
"Col 3": 47,
"Col 4": 5,
....
},
....
]
}
]
我的目標是將其轉換為以下內容:
newDataSet = [{
fixedValue: [
{
name: "SD",
fixedValue: [
{ name: "Col 2", value: 208 },
{ name: "Col 3", value: 88 },
{ name: "Col 4", value: 4 },
...
]
},
{
name: "EFG",
fixedValue: [
{ name: "Col 2", value: 106 },
{ name: "Col 3", value: 46 },
{ name: "Col 4", value: 4 },
...
]
},
{
name: "ERG",
fixedValue: [
{ name: "Col 2", value: 107 },
{ name: "Col 3", value: 47 },
{ name: "Col 4", value: 5 },
...
]
},
....
]}
]
它獲取 originalDataSet 中的每個“類別”值,並將其作為一個名為“名稱”的新鍵的值,將非“類別”或“總計”的鍵和鍵值分配給新鍵。
到目前為止,我有這樣的事情,但我覺得它的方向是錯誤的:
// First work with the array in originalDataSet.fixedValue
const newObj = {};
// For each object in the fixedValue array
for (const key of originalDataSet.fixedValue) {
// If the key does not equal to 'category' or 'total' e.g., in this example we're referring to 'Col 2', 'Col 3', and 'Col 4', though there could be more.
if(!key['category'] && !key['total'])
// Should yield something like this: { name: "Col 2", value: 107 }
newObj['name'] = key;
newObj['value'] = newObj[key];
}
}
originalDataSet.fixedValue.foreach(
newDataSet = [{
fixedValue: [
name: item.category
fixedValue: [newObj]
]
}]
}
我將如何實現 newDataSet?
You can use destructuring to isolate the category
and total
properties from the rest, and then use Object.entries
on that rest object to map those key/value pairs to little {name, value}
objects:
const convert = arr => arr.map(({fixedValue}) => ({ fixedValue: fixedValue.map(({category, total, ...rest}) => ({ name: category, fixedValue: Object.entries(rest).map(([name, value]) => ({ name, value })) })) })); const originalDataSet = [{"fixedValue": [{"category": "SD","total": 2,"Col 2": 208,"Col 3": 88,"Col 4": 4,}, {"category": "EFG","total": 1,"Col 2": 106,"Col 3": 46,"Col 4": 4,}, {"category": "ERG","total": 1,"Col 2": 107,"Col 3": 47,"Col 4": 5,}]}] console.log(convert(originalDataSet))
此數據看起來像 JSON 您可以通過以下方法輕松轉換它:
const originalDataSet = {
"fixedValue": [{
"category": "SD",
"total": 2,
"Col 2": 208,
"Col 3": 88,
"Col 4": 4
},
{
"category": "EFG",
"total": 1,
"Col 2": 106,
"Col 3": 46,
"Col 4": 4
},
{
"category": "ERG",
"total": 1,
"Col 2": 107,
"Col 3": 47,
"Col 4": 5
}
]
}
const obj = JSON.stringify(originalDataSet);
const obj1 = JSON.parse(obj)
const newObj = obj1.fixedValue.map(el => {
const newObject={
name:el.category,
fixedValue:[]
}
newObject.fixedValue.push({name:"Col 2", value:el["Col 2"]})
newObject.fixedValue.push({name:"Col 3", value:el["Col 3"]})
newObject.fixedValue.push({name:"Col 4", value:el["Col 4"]})
return newObject
})
可以使用循環改進代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.