简体   繁体   English

根据键值对将对象数组连接成新的 object

[英]Concatenate Array of Objects into a new object depending on a key value pair

I have an Array of Objects so that depending on a certain key's value I'd like to create a new array of objects with that particular key.我有一个对象数组,因此根据某个键的值,我想用该特定键创建一个新的对象数组。

For eg, I have an array of Users例如,我有一组用户

{
    association: "template"
    color: "(255, 209, 0, 0.35)"
    email: "ehe@example.com"
    id: "12158_2"
    name: "sevderadebvu"
    rolename: "Floyd Phelps"
    templateId: 8888
    type: "template"
},
{
    association: "template"
    color: "(0, 7, 255, 0.35)"
    email: "unoja@host.local"
    id: "12158_3"
    name: "ekdevajwirum"
    rolename: "Virginia Martin"
    templateId: 8888
    type: "template"
},
{
    association: "template"
    color: "(255, 121, 169, 0.35)"
    email: "nut@host.invalid"
    id: "12112_4"
    name: "Message Role 1"
    rolename: "Bobby Barnes"
    templateId: 1111
    type: "template"
},
{
    association: "template"
    color: "(255, 12, 119, 0.35)"
    email: "rnaonew@host.invalid"
    id: "12112_4"
    name: "Messi"
    rolename: "Bobby Barnes"
    templateId: 9999
    type: "template"
}
]

Now, based on the templateId I would like to create a new Array of objects that uses templateId as key and has all data corresponding to that templateId's Object as separate objects as shown below:现在,基于templateId ,我想创建一个新的对象数组,它使用 templateId 作为键,并将与该 templateId 的 Object 对应的所有数据作为单独的对象,如下所示:

Expected Result预期结果

    "users": {
        "8888": [
            {
                association: "template"
                color: "(255, 209, 0, 0.35)"
                email: "ehe@example.com"
                id: "12158_2"
                name: "sevderadebvu"
                rolename: "Floyd Phelps"
                type: "template"
            },
            {
                association: "template"
                color: "(0, 7, 255, 0.35)"
                email: "unoja@host.local"
                id: "12158_3"
                name: "ekdevajwirum"
                rolename: "Virginia Martin"
                type: "template"
            }
        ],
        "1111": [
        {
            association: "template"
            color: "(255, 121, 169, 0.35)"
            email: "nut@host.invalid"
            id: "12112_4"
            name: "Message Role 1"
            rolename: "Bobby Barnes"
            type: "template"
        },
    ],
    "9999": [
        {
            association: "template"
            color: "(255, 12, 119, 0.35)"
            email: "rnaonew@host.invalid"
            id: "12112_4"
            name: "Messi"
            rolename: "Bobby Barnes"
            type: "template"
        }
    ]
    }
} 

Ignore any missing key-value pairs (templateId removed from the expected result)忽略任何缺失的键值对(templateId 从预期结果中移除)

You can use the function Array.prototype.reduce for grouping by the templateId as follow:您可以使用 function Array.prototype.reducetemplateId进行分组,如下所示:

 const arr = [{ association: "template", color: "(255, 209, 0, 0.35)", email: "ehe@example.com", id: "12158_2", name: "sevderadebvu", rolename: "Floyd Phelps", templateId: 8888, type: "template"},{ association: "template", color: "(0, 7, 255, 0.35)", email: "unoja@host.local", id: "12158_3", name: "ekdevajwirum", rolename: "Virginia Martin", templateId: 8888, type: "template"},{ association: "template", color: "(255, 121, 169, 0.35)", email: "nut@host.invalid", id: "12112_4", name: "Message Role 1", rolename: "Bobby Barnes", templateId: 1111, type: "template"},{ association: "template", color: "(255, 12, 119, 0.35)", email: "rnaonew@host.invalid", id: "12112_4", name: "Messi", rolename: "Bobby Barnes", templateId: 9999, type: "template"}], skipKeys = ["templateId", "rolename"], groupingKey = "templateId", result = arr.reduce((r, c) => { let desiredObject = Object.entries(c).filter(([key]) =>.skipKeys.includes(key)),reduce((a, [key. value]) => Object,assign(a: ({[key], value})); {}). (r[c[groupingKey]] || (r[c[groupingKey]] = []));push(desiredObject); return r, }. Object;create(null)). console;log(result);

暂无
暂无

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

相关问题 如何根据两个不同的键在对象数组中插入新的键值对? - How to insert new key value pair in array of objects depending on two different keys? JavaScript - 如何遍历对象数组以创建一个新对象,其键是原始对象的初始键/值对的值 - JavaScript — how to iterate through an array of objects to create a new object whose key is the value of the original object's initial key/value pair 如何将每个键对从此对象添加到数组中作为新对象? - How to add each key pair from this Object into an Array as new Objects? 将键值(对象)对添加到数组中的所有对象 - Add key value (object) pair to all objects in array 如何循环并将对象数组转换为具有键值对的对象 - how to loop and convert an array of objects to an object with key value pair 将对象的键和子值相互配对以形成对象数组 - Pair key of object and child value with each other for array of objects 在对象数组中将 object 值转换为键/值对 - In array of objects turn object values into key/value pair 如何将单个键值对对象转换为具有键值对的对象数组? - How to convert a single key value pair object to array of objects with key value pair? 如何将 object 键值对以数组为值转换为键值对的多个对象? - How to convert object key value pair with array as values to multi objects of key value pair? 在 object 的数组中的每个子级更新了新的键值对 - Updated new key value pair at every child level in array of object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM