我想按評估單元 ID 拆分物件。
我有這個
{
"name": "test",
"description": "test",
"asessment_units[]": ["2", "4", "5","8"]
}
從這個物件中我想要四個物件(因為我在評估單元中有 4 個元素)。
第一個物件
{
"name": "test",
"description": "test",
"asessment_units[]": ["2"]
}
第二個物件
{
"name": "test",
"description": "test",
"asessment_units[]": ["4"]
}
第三個物件
{
"name": "test",
"description": "test",
"asessment_units[]": ["5"]
}
第四個物件
{
"name": "test",
"description": "test",
"asessment_units[]": ["8"]
}
uj5u.com熱心網友回復:
也許你可以使用and :Array.prototype.mapObject.assign
const obj = {
name: "test",
description: "test",
"asessment_units[]": ["2", "4", "5", "8"],
};
const splitObjs = obj["asessment_units[]"].map(unit =>
Object.assign({}, obj, {
"asessment_units[]": [unit],
})
);
console.log(splitObjs);
uj5u.com熱心網友回復:
你可以使用Array.prototype.map()onasessment_units[]來實作轉換。物件的傳播name和description鍵是通用的,并將各自的值插入到回呼內部的陣列中map。
const obj = {
"name": "test",
"description": "test",
"asessment_units[]": ["2", "4", "5","8"]
}
const res = obj['asessment_units[]'].map((val,index)=>{
return {...obj, ['asessment_units[]'] : [val]}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/466275.html
標籤:javascript 数组 目的
上一篇:如何將兩個foreach回圈替換為一個賦值(mb兩個array_map函式)?
下一篇:從文本框向串列框添加字串
