Javascript:将两个数组的值映射到一个新数组中

Javascript:将两个数组的值映射到一个新数组中,javascript,arrays,Javascript,Arrays,我有两个对象数组,如下所示: const data = [ { id: 1, condition: 1, helpers: [{id: 1, condition: null}, {id: 2, condition: 2}] }, { id: 2, condition: null, helpers: [{id: 1, condition: 1}, {id: 2, condition: null}] } ] const conds =

我有两个对象数组,如下所示:

const data = [
  {
    id: 1,
    condition: 1,
    helpers: [{id: 1, condition: null}, {id: 2, condition: 2}]
  },
  {
    id: 2,
    condition: null,
    helpers: [{id: 1, condition: 1}, {id: 2, condition: null}]
  }
]

const conds = [
  {
    id: 1,
    conditions: {
       rules: ['test', 'foo']
    }
  },
  {
    id: 2,
    conditions: {
       rules: ['#hashtag', 'foo']
    }
  }
]
let newArray = [];

data.forEach(obj => {
        conds.forEach(cond => {
            if (obj.condition) {
                if (obj.condition === cond.id) {
                    obj.condition = cond.conditions.rules;
                    newArray.push(obj);
                }
            } else {
                obj.helpers.forEach(h => {
                    if (h.condition && h.condition === cond.id) {
                        h.condition = cond.conditions.rules;
                        newArray.push(obj);
                    }
                });
            }
        })
    });
[
  {
     id: 1
     condition: ['test', 'foo'],
     helpers: [{id: 1, condition: null}, {id: 2, condition: ['#hashtag', 'foo']}]
  },
  {
     id: 2
     condition: null,
     helpers: [{id: 1, condition: ['test', 'foo']}, {id: 2, condition: null}]
  },
]
我试图实现的是,我想用
conds
数组中的值替换
数据
数组的
条件

我的解决方案效果不佳,如下所示:

const data = [
  {
    id: 1,
    condition: 1,
    helpers: [{id: 1, condition: null}, {id: 2, condition: 2}]
  },
  {
    id: 2,
    condition: null,
    helpers: [{id: 1, condition: 1}, {id: 2, condition: null}]
  }
]

const conds = [
  {
    id: 1,
    conditions: {
       rules: ['test', 'foo']
    }
  },
  {
    id: 2,
    conditions: {
       rules: ['#hashtag', 'foo']
    }
  }
]
let newArray = [];

data.forEach(obj => {
        conds.forEach(cond => {
            if (obj.condition) {
                if (obj.condition === cond.id) {
                    obj.condition = cond.conditions.rules;
                    newArray.push(obj);
                }
            } else {
                obj.helpers.forEach(h => {
                    if (h.condition && h.condition === cond.id) {
                        h.condition = cond.conditions.rules;
                        newArray.push(obj);
                    }
                });
            }
        })
    });
[
  {
     id: 1
     condition: ['test', 'foo'],
     helpers: [{id: 1, condition: null}, {id: 2, condition: ['#hashtag', 'foo']}]
  },
  {
     id: 2
     condition: null,
     helpers: [{id: 1, condition: ['test', 'foo']}, {id: 2, condition: null}]
  },
]
我觉得我非常接近解决方案,因为我的
newArray
包含changes属性,但不是helpers中的最后一项,而
condition
属性仍然是2

输出应如下所示:

const data = [
  {
    id: 1,
    condition: 1,
    helpers: [{id: 1, condition: null}, {id: 2, condition: 2}]
  },
  {
    id: 2,
    condition: null,
    helpers: [{id: 1, condition: 1}, {id: 2, condition: null}]
  }
]

const conds = [
  {
    id: 1,
    conditions: {
       rules: ['test', 'foo']
    }
  },
  {
    id: 2,
    conditions: {
       rules: ['#hashtag', 'foo']
    }
  }
]
let newArray = [];

data.forEach(obj => {
        conds.forEach(cond => {
            if (obj.condition) {
                if (obj.condition === cond.id) {
                    obj.condition = cond.conditions.rules;
                    newArray.push(obj);
                }
            } else {
                obj.helpers.forEach(h => {
                    if (h.condition && h.condition === cond.id) {
                        h.condition = cond.conditions.rules;
                        newArray.push(obj);
                    }
                });
            }
        })
    });
[
  {
     id: 1
     condition: ['test', 'foo'],
     helpers: [{id: 1, condition: null}, {id: 2, condition: ['#hashtag', 'foo']}]
  },
  {
     id: 2
     condition: null,
     helpers: [{id: 1, condition: ['test', 'foo']}, {id: 2, condition: null}]
  },
]

这里我遗漏了什么?

使用循环通过
数据
数组获取
id
,然后使用过滤
条件
数组以获取相关的
条件。规则如下:

data.forEach(function(item) {
    item.condition = conds.find(x => x.id === item.id).conditions.rules;
});
工作小提琴:

const数据=[{
id:1,
条件:1,,
帮助程序:[{id:1,条件:null},{id:2,条件:2}]
},{
id:2,
条件:空,
帮助程序:[{id:1,条件:1},{id:2,条件:null}]
}];
常数条件=[{
id:1,
条件:{
规则:['test','foo']
}
},{
id:2,
条件:{
规则:['#hashtag',foo']
}
}];
data.forEach(功能(项){
item.condition=conds.find(x=>x.id==item.id).conditions.rules;
});
控制台日志(数据)
const数据=[
{
id:1,
条件:1,,
帮助程序:[{id:1,条件:null},{id:2,条件:2}]
},
{
id:2,
条件:空,
帮助程序:[{id:1,条件:1},{id:2,条件:null}]
}
]
常数条件=[
{
id:1,
条件:{
规则:['test','foo']
}
},
{
id:2,
条件:{
规则:['#hashtag',foo']
}
}
]
//起始溶液
常量规则映射=条件还原((映射,条件)=>{
map[condition.id]=condition.conditions.rules;
返回图;
}, {});
const finalData=data.map(dataItem=>{
dataItem.condition=dataItem.condition?规则映射[dataItem.condition]:空;
返回数据项;
});
//最终解决方案
console.log(finalData)使其正常工作:

const finalData = data.map(dataItem => {
  dataItem.condition = dataItem.condition ? rulesMap[dataItem.condition] : null;
  dataItem.helpers.map(item => {
    item.condition = item.condition ? rulesMap[item.condition] : null
  })
  return dataItem;
});

预期的输出格式是什么?看起来很像。您只需循环查看
数据
,获取
帮助程序
,然后执行我链接到的问题答案中列出的合并。@Eddie我在主问题中添加了输出。每次使用“查找”时,在循环内创建另一个循环。想象两个巨大的阵列,它将永远完成。正如我在回答中所写,我认为绘制条件图是一种成本较低的方法。嘿@zakaria,不幸的是,这不是一个有效的例子。
helpers
数组中的条件也应该映射到条件。我得到的错误是
减少没有定义在这里:/I我刚刚更改了映射构造函数以使用更少的内存<代码>规则映射
在这里更有意义,因为我们不关心其他属性。嘿@Bruno-不幸的是,这不是我所期望的解决方案。请参阅我问题中的预期输出-实际上,您的解决方案返回的结果与我的版本相同。:/
helpers
数组中的条件也应映射到条件。使用规则映射处理此逻辑:
const finalData=data.map(dataItem=>{dataItem.condition=dataItem.condition?rulesmap[dataItem.condition]:null;dataItem.helpers.map(item=>{item.condition=item.condition?规则映射[item.condition]:null})返回数据项;});