通过另一个预定义的Array元素作为键来重构JavaScript对象数组



我在jsbin上也放了相同的代码:https://jsbin.com/literefeqo/edit?js,控制台

解释

我有一个对象数组(1(,想变换(可能使用贴图(这个对象。转换标准是给定的数组(2(,并且对应于arrObj中的german属性。这意味着,如果arrObj中有german属性,则应将其"复制"出来,并将其用作生成resultObj(3(的键。如果没有german属性,则密钥应为"未知"或其他类型。

注意:resultObj中可以有更多的条目,例如Montag。这样resultObj.Montag[i]应该是一个对象数组。

(1(对象阵列

const arrObj = [
{
"day": {
"string": "Monday",
"Number": 1
},
"description": {
"type": "string",
"value": "The first day of the week"
},
"german": {
"type": "string",
"value": "Montag"
}
},
{
"day": {
"string": "Tuesday",
"Number": 2
},
"description": {
"type": "string",
"value": "The second day of the week"
}
},
{
"day": {
"string": "Wednesday",
"Number": 3
},
"description": {
"type": "string",
"value": "The third day of the week"
},
"german": {
"type": "string",
"value": "Mittwoch"
}
}
];

(2(应该成为新对象密钥的数组

const germanDays = ["Montag","Dienstag","Mittwoch","Donnerstag"];

(3(结果应类似

const resultObj =   {
"Montag": [
{
"day": {
"string": "Monday",
"Number": 1
},
"description": {
"type": "string",
"value": "The first day of the week"
},
"german": {
"type": "string",
"value": "Montag"
}
}
],
"Dienstag": [
{}
],
"Mittwoch": [
{
"day": {
"string": "Wednesday",
"Number": 3
},
"description": {
"type": "string",
"value": "The third day of the week"
},
"german": {
"type": "string",
"value": "Mittwoch"
}
}
],
"Donnerstag": [
{}
],
"Unknown": [
{
"day": {
"string": "Tuesday",
"Number": 2
},
"description": {
"type": "string",
"value": "The second day of the week"
}
}
]

};

(可能使用映射(

map,用于数组到数组的映射,一个更合适的函数是reduce。

下面是一个例子。

const germanDays = ["Montag","Dienstag","Mittwoch","Donnerstag"]
const arrObj = [
{
"day": {
"string": "Monday",
"Number": 1
},
"description": {
"type": "string",
"value": "The first day of the week"
},
"german": {
"type": "string",
"value": "Montag"
}
},
{
"day": {
"string": "Tuesday",
"Number": 2
},
"description": {
"type": "string",
"value": "The second day of the week"
}
},
{
"day": {
"string": "Wednesday",
"Number": 3
},
"description": {
"type": "string",
"value": "The third day of the week"
},
"german": {
"type": "string",
"value": "Mittwoch"
}
},
{
"day": {
"string": "Monday",
"Number": 1
},
"description": {
"type": "string",
"value": "Just another text is here"
},
"german": {
"type": "string",
"value": "Montag"
}
}
];
const ret = germanDays.reduce((a, v) => {
const f = arrObj.filter(f => f.german && f.german.value === v);
a[v] = f;
return a;
}, {
"Unknown": arrObj.filter(f => !f.german)
});
console.log(ret);

类似的内容(单击"运行代码片段"按钮(:

const arrObj = [
{
"day": {
"string": "Monday",
"Number": 1
},
"description": {
"type": "string",
"value": "The first day of the week"
},
"german": {
"type": "string",
"value": "Montag"
}
},
{
"day": {
"string": "Tuesday",
"Number": 2
},
"description": {
"type": "string",
"value": "The second day of the week"
}
},
{
"day": {
"string": "Wednesday",
"Number": 3
},
"description": {
"type": "string",
"value": "The third day of the week"
},
"german": {
"type": "string",
"value": "Mittwoch"
}
}
];
const germanDays = ["Montag", "Dienstag", "Mittwoch", "Donnerstag"];
const resultObj = {}
for (const item of arrObj) {
if (item.german && item.german.value && germanDays.includes(item.german.value)) {
addVal(item.german.value, item)
} else {
addVal('unknown', item)
}
}
// helper func
function addVal(key, val) {
if(!resultObj[key]) {
resultObj[key] = []
}
resultObj[key].push(val)
}
console.log(resultObj)

最新更新