所以我有这个数组
var mapped = [[2016, "October", "Monday", {object}], [2017, "January", "Friday", {object}], [2017, "January", "Wednesday", {object}], [2017, "October", "Monday", {object}]]
我想要完成的是这样的事情:
[{
"2016": [{
"October": [{
"Monday": [{object}]
}]
}],
}, {
"2017": [{
"January": [{
"Friday": [{object}]
}, {
"Wednesday": [{object}]
}]
}, {
"October": [{
"Monday": [{object}]
}]
}]
}]
我一直在寻找这么久,我找不到解决方案..通过使用reduce,我得到的是这样的:
[
2016: [{
"month": "October"
}]
],
[
2017: [{
"month": "January"
},
{
"month": "January"
},
{
"month": "September"
}]
]
所以看起来我已经融入了某些东西,但仍然遥远......这就是我正在做的事情:
mapped.reduce((years, array) => {
years[array[0]] = years[array[0]] || [];
years[array[0]].push({
month: array[1]
})
return years;
}, [])
答案 0 :(得分:2)
不完全是您指定的内容,但我觉得以下脚本输出的格式最有用 - 它只生成最深层的数组:
const mapped = [[2016, "October", "Monday", { a: 1 }], [2017, "January", "Friday", { a: 1 }], [2017, "January", "Wednesday", { a: 1 }], [2017, "October", "Monday", { a: 1 }]];
const result = mapped.reduce( (acc, [year, month, day, object]) => {
let curr = acc[year] = acc[year] || {};
curr = curr[month] = curr[month] || {};
curr = curr[day] = curr[day] || [];
curr.push(object);
return acc;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
如果您确实需要包装数组,可以对前一个结果应用额外的递归函数:
const mapped = [[2016, "October", "Monday", { a: 1 }], [2017, "January", "Friday", { a: 1 }], [2017, "January", "Wednesday", { a: 1 }], [2017, "October", "Monday", { a: 1 }]];
const result = mapped.reduce( (acc, [year, month, day, object]) => {
let curr = acc[year] = acc[year] || {};
curr = curr[month] = curr[month] || {};
curr = curr[day] = curr[day] || [];
curr.push(object);
return acc;
}, {});
function wrapInArrays(data) {
return Array.isArray(data) ? data
: Object.entries(data).map ( ([key, value]) => {
return { [key]: wrapInArrays(value) };
});
}
const wrapped = wrapInArrays(result);
console.log(wrapped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
要获得完全相同的结构,您可以尝试以下策略。这将适用于任何级别的嵌套,并将继续构建嵌套对象。请看一下这个例子,看看将要处理的各种嵌套级别。
将每个元素映射到相应的嵌套结构(Map with reduce)
将映射数组中的每个元素合并为一个对象以合并所有冗余键
将最终的嵌套对象转换为所需的数组结构。
const mapped = [[2016, "October", "Monday", "Morning", {time: "10AM"}], [2017, "January", "Friday", {object: "OBJECT"}], [2017, "January", "Wednesday", {object: "OBJECT"}], [2017, "October", "Monday", {object: "OBJECT"}]];
// Map all objects to the respective nested structure
const nestedMap = mapped.map(elem => {
let reversed = [...elem.reverse()];
let firstElem = reversed.splice(0, 1)[0];
return reversed.reduce((acc, elem) => {let newAcc = {}; newAcc[elem] = [acc]; return {...newAcc}}, firstElem);
})
// Combine array to form a single object
const nestedObj = nestedMap.reduce((acc, elem) => {
let firstKey = Object.keys(elem)[0];
acc[firstKey]
?
acc[firstKey].push(elem[firstKey][0])
:
acc[firstKey] = elem[firstKey];
return acc;
}, {})
// Convert back into the array with each object as element
const nestedFinalArr = Object.keys(nestedObj).map(key => ({[key]: nestedObj[key]}))
console.log(nestedFinalArr);