简体   繁体   English

Javascript - 根据公共值重构对象数组

[英]Javascript - restructure array of objects according to common value

My input array of objects it is like this, I have an input array that looks like this :我的对象输入数组是这样的,我有一个如下所示的输入数组:

[{"id": 657, "name": "Amadeus Basin", "contenttype": "province"}
{"id": 1173, "name": "Amazonas Basin", "contenttype": "province"}
{"id": 373, "name": "American Samoa", "contenttype": "countries"}
{"id": 1, "name": "Samoa", "contenttype": "marine"}
{"id": 796, "name": "Amhara", "contenttype": "province"}]

What I would like to achieve it would be:我想实现的是:

[
    {
    "contenttype": "province",
    "content" : [
        {
            "id": 657,
            "name": "Amadeus Basin"
        },
        {
            "id": 1173,
            "name": "Amazonas Basin"
        },
        {
            "id": 796,
            "name": "Amhara"
        }
    ]
    }, {

        "contenttype": "countries",
        "content": [
            {
                "id": 373,
                "name" : "American Samoa"
            }
        ]

    },
    {
        "contenttype": "countries",
        "content": [
        {
            "id": 1,
            "name" : "Samoa"
        }
    ]

    }

]

splitting objects in the array according to one value and creating sub-arrays of the same objects inside these newly created objects.根据一个值拆分数组中的对象,并在这些新创建的对象中创建相同对象的子数组。 It would be good also using lodash.使用 lodash 也会很好。

You can use reduce function to iterate over each item and then check it existence in the array, if not found add new one, if found - push into it您可以使用reduce函数迭代每个项目,然后检查它是否存在于数组中,如果没有找到,则添加新的,如果找到 - 推入它

 const data = [{"id": 657, "name": "Amadeus Basin", "contenttype": "province"}, {"id": 1173, "name": "Amazonas Basin", "contenttype": "province"}, {"id": 373, "name": "American Samoa", "contenttype": "countries"}, {"id": 1, "name": "Samoa", "contenttype": "marine"}, {"id": 796, "name": "Amhara", "contenttype": "province"}]; const grouped = data.reduce((acc, item) => { const found = acc.find(i => i.contenttype === item.contenttype); const content = {id: item.id, name: item.name}; if(found) { found.content.push(content); } else { acc.push({contenttype: item.contenttype, content: [content] }) } return acc; }, []); console.log(grouped);

You could use _.groupBy and map the wanted properties.您可以使用_.groupBy并映射所需的属性。

 var data = [{ id: 657, name: "Amadeus Basin", contenttype: "province" }, { id: 1173, name: "Amazonas Basin", contenttype: "province" }, { id: 373, name: "American Samoa", contenttype: "countries" }, { id: 1, name: "Samoa", contenttype: "marine" }, { id: 796, name: "Amhara", contenttype: "province" }], result = _(data) .groupBy('contenttype') .map((value, key) => ({ contenttype: key, content: _.map(value, v => _.pick(v, ['id', 'name'])) })) .value(); console.log(result);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM