繁体   English   中英

根据另一个对象中的属性向对象添加属性数组

[英]Add array of properties to object based on properties in another object

我正在处理大量对象。 通过过滤,我缩小了数据集。 我需要根据另一个“查找”对象中的属性向每个对象添加一组属性。 我的初始对象数组看起来像这样。

const arr = [{id: "12345", type: "square", current: "0", max: "1", code: "50"}, 
             {id: "23456", type: "square", current: "0", max: "3", code: "50"},
             {id: "54321", type: "square", current: "2", max: "4", code: "50"},
             {id: "54321", type: "circle", current: "0", max: "1", code: "100"}, 
             {id: "65432", type: "circle", current: "3", max: "3", code: "100"},
             {id: "76543", type: "circle", current: "0", max: "2", code: "100"}]

我有一个“查找”对象。 对象的基础是每个typecode都有一个level 0 到 3。对于上面的每个对象,我需要为每个匹配typecode id添加title属性,从currentmax 对于这个例子,我简化了“查找”对象。

const lookup = [{title: "yes", code: "50", level: "0", type: "square"},
                {title: "no", code: "50", level: "1", type: "square"}, 
                {title: "maybe", code: "50", level: "2", type: "square"},
                {title: "sure", code: "50", level: "3", type: "square"},
                {title: "up", code: "100", level: "0", type: "circle"},
                {title: "down", code: "100", level: "1", type: "circle"},
                {title: "left", code: "100", level: "2"}, type: "circle"},
                {title: "right", code: "100", level: "3"}, type: "circle"}]

所以我生成的对象数组看起来像这样

const result = [{id: "12345", type: "square", current: "0", max: "1", code: "50", titles: ["yes", "no"]}, 
                {id: "23456", type: "square", current: "0", max: "3", code: "50", titles: ["yes", "no", "maybe", "sure"]},
                {id: "54321", type: "square", current: "2", max: "4", code: "50", titles: ["maybe", "sure"]},
                {id: "54321", type: "circle", current: "0", max: "2", code: "100", titles: ["up", "down"]}, 
                {id: "65432", type: "circle", current: "3", max: "3", code: "100", titles: ["right"]},
                {id: "76543", type: "circle", current: "0", max: "2", code: "100", titles: ["up", "down", "left"]}]

有没有一种优雅的方法来做到这一点,而无需遍历typecodelevel每一个组合?

首先,让我们将lookup数组转换为嵌套树,这样查找标题将非常简单:

const lookupTree = lookup.reduce((acc, o) => {
    var byType = acc[o.type] = acc[o.type] || {};
    var byCode = byType[o.code] = byType[o.code] || {};
    byCode[o.level] = o.title;
    return acc;
}, {});

这将导致如下树结构:

{
   "square": {
      "50": {
         "0": "yes",
         "1": "no",
         "2": "maybe",
         "3": "sure"
      }
   },
   "circle": {
      "100": {
         "0": "up",
         "1": "down",
         "2": "left",
         "3": "right"
      }
   }
}

现在对于数组arr中的每个对象,我们只需从currentmax (包括)循环并将每个级别映射到其等效标题,通过首先按类型然后按代码最后按级别简单遍历上面的树(我们首先检查那些存在,即如果查找树中的当前对象有一个类型,如果有它的代码并且也有一个级别):

arr.forEach(o => {
    o.titles = [];
    for(var i = o.current; i <= o.max; i++) {
        if(lookupTree[o.type] && lookupTree[o.type][o.code] && lookupTree[o.type][o.code][i]) {
            o.titles.push(lookupTree[o.type][o.code][i]);
        }
    }
});

例子:

 const arr = [{"id":"12345","type":"square","current":"0","max":"1","code":"50"},{"id":"23456","type":"square","current":"0","max":"3","code":"50"},{"id":"54321","type":"square","current":"2","max":"4","code":"50"},{"id":"54321","type":"circle","current":"0","max":"1","code":"100"},{"id":"65432","type":"circle","current":"3","max":"3","code":"100"},{"id":"76543","type":"circle","current":"0","max":"2","code":"100"}]; const lookup = [{"title":"yes","code":"50","level":"0","type":"square"},{"title":"no","code":"50","level":"1","type":"square"},{"title":"maybe","code":"50","level":"2","type":"square"},{"title":"sure","code":"50","level":"3","type":"square"},{"title":"up","code":"100","level":"0","type":"circle"},{"title":"down","code":"100","level":"1","type":"circle"},{"title":"left","code":"100","level":"2","type":"circle"},{"title":"right","code":"100","level":"3","type":"circle"}]; const lookupTree = lookup.reduce((acc, o) => { var byType = acc[o.type] = acc[o.type] || {}; var byCode = byType[o.code] = byType[o.code] || {}; byCode[o.level] = o.title; return acc; }, {}); arr.forEach(o => { o.titles = []; for(var i = o.current; i <= o.max; i++) { if(lookupTree[o.type] && lookupTree[o.type][o.code] && lookupTree[o.type][o.code][i]) { o.titles.push(lookupTree[o.type][o.code][i]); } } }); console.log(arr);

注意:如果您不想更改原始数组arr ,则只需使用map而不是forEach并在将title属性添加到对象之前克隆对象。

暂无
暂无

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

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