JS:将数组减少为嵌套对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS:将数组减少为嵌套对象相关的知识,希望对你有一定的参考价值。

所以我有这个阵列

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;

                }, [])
答案

不完全是你指定的,但我觉得以下脚本输出的格式最有用 - 它只生成最深层次的数组:

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; }
另一答案

要获得完全相同的结构,您可以尝试以下策略。这将适用于任何级别的嵌套,并将继续构建嵌套对象。请查看示例,以查看将进行一般处理的各种嵌套级别。

  • 将每个元素映射到相应的嵌套结构(使用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);

以上是关于JS:将数组减少为嵌套对象的主要内容,如果未能解决你的问题,请参考以下文章

在JS中将对象数组减少为hashmap

为什么我不能在此片段中生成唯一对象数组?

jQuery与lodash($_)

通过键减少数组对象并返回嵌套对象

如何使用 Node.js 和 Mongoose 将对象添加到嵌套数组

如何在 Ios 中解析数组数据中的嵌套 Json 对象