Javascript JS。更新数组中的对象(按值匹配)
第一个数组(a)和第二个数组(b)具有更新的值。需要通过IDs对象更新匹配并获得结果数组(c)。我怎样才能使它简单快速Javascript JS。更新数组中的对象(按值匹配),javascript,arrays,loops,Javascript,Arrays,Loops,第一个数组(a)和第二个数组(b)具有更新的值。需要通过IDs对象更新匹配并获得结果数组(c)。我怎样才能使它简单快速 let a = [ { id: 1, activated: '0' }, { id: 2, activated: '0' }, { id: 3, activated: '0' }, { id: 4, activated: '0' }, ] let b = [ { id: 2, activated: '1' }, { id: 3
let a = [
{ id: 1, activated: '0' },
{ id: 2, activated: '0' },
{ id: 3, activated: '0' },
{ id: 4, activated: '0' },
]
let b = [
{ id: 2, activated: '1' },
{ id: 3, activated: '1' },
]
//Result array:
c = [
{ id: 1, activated: '0' },
{ id: 2, activated: '1' },
{ id: 3, activated: '1' },
{ id: 4, activated: '0' },
]
试试这个
a=[{
id:1,
已激活:“0”
},
{
id:2,
已激活:“0”
},
{
id:3,
已激活:“0”
},
{
id:4,
已激活:“0”
},
]
设b=[{
id:2,
已激活:“1”
},
{
id:3,
已激活:“1”
},
]
//结果数组:
c=[{
id:1,
已激活:“0”
},
{
id:2,
已激活:“1”
},
{
id:3,
已激活:“1”
},
{
id:4,
已激活:“0”
},
]
让结果=a.map(e=>{
让p=b.find(be=>be.id==e.id)
如果(p)e.激活=p.激活;
返回e
})
console.log(结果)
试试这个
a=[{
id:1,
已激活:“0”
},
{
id:2,
已激活:“0”
},
{
id:3,
已激活:“0”
},
{
id:4,
已激活:“0”
},
]
设b=[{
id:2,
已激活:“1”
},
{
id:3,
已激活:“1”
},
]
//结果数组:
c=[{
id:1,
已激活:“0”
},
{
id:2,
已激活:“1”
},
{
id:3,
已激活:“1”
},
{
id:4,
已激活:“0”
},
]
让结果=a.map(e=>{
让p=b.find(be=>be.id==e.id)
如果(p)e.激活=p.激活;
返回e
})
console.log(result)
我建议您创建一个对象,其中键是快速访问的ID
使用新创建的对象,您可以使用id
作为键从b
访问特定对象并更新对象
最后,使用函数Object.values
获取数组c
这是假设b
的id存在于a
中
let a=[{id:1,激活:'0'},{id:2,激活:'0'},{id:3,激活:'0'},{id:4,激活:'0'}]
设b=[{id:2,激活:'1'},{id:3,激活:'1'}];
设newA=a.reduce((a,{id,{rest})=>({a,{[id]:{id,{rest}}),{});
b、 forEach(({id,activated})=>newA[id]。activated=activated);
设c=Object.values(newA);
控制台日志(c)代码>
.as console wrapper{max height:100%!important;top:0;}
我建议您创建一个对象,其中键是快速访问的ID
使用新创建的对象,您可以使用id
作为键从b
访问特定对象并更新对象
最后,使用函数Object.values
获取数组c
这是假设b
的id存在于a
中
let a=[{id:1,激活:'0'},{id:2,激活:'0'},{id:3,激活:'0'},{id:4,激活:'0'}]
设b=[{id:2,激活:'1'},{id:3,激活:'1'}];
设newA=a.reduce((a,{id,{rest})=>({a,{[id]:{id,{rest}}),{});
b、 forEach(({id,activated})=>newA[id]。activated=activated);
设c=Object.values(newA);
控制台日志(c)代码>
.as console wrapper{max height:100%!important;top:0;}
您可以创建第二个数组的映射,由id
键入,然后在将a
对象复制到c
中时,使用该映射将匹配的b
对象合并到它们中
let a=[{id:1,激活:'0'},{id:2,激活:'0'},{id:3,激活:'0'},{id:4,激活:'0'}]
设b=[{id:2,激活:'1'},{id:3,激活:'1'}];
设map=newmap(b.map(o=>[o.id,o]);
设c=a.map(o=>({…o,…map.get(o.id)}));
控制台日志(c)代码>
.as console wrapper{max height:100%!important;top:0;}
您可以创建第二个数组的映射,由id
键入,然后在将a
对象复制到c
中时,使用该映射将匹配的b
对象合并到它们中
let a=[{id:1,激活:'0'},{id:2,激活:'0'},{id:3,激活:'0'},{id:4,激活:'0'}]
设b=[{id:2,激活:'1'},{id:3,激活:'1'}];
设map=newmap(b.map(o=>[o.id,o]);
设c=a.map(o=>({…o,…map.get(o.id)}));
控制台日志(c)代码>
.as控制台包装{max height:100%!important;top:0;}
从数组a的ID构建哈希。通过在散列中查找索引b.id来迭代数组b,如果在a中找到索引b.id,则已激活已建立的索引属性。否则(在示例id:5中),旧数组中不存在该id的条目,因此创建一个新的条目并调整哈希大小
a=[
{id:1,已激活:'0'},
{id:2,已激活:'0'},
{id:3,已激活:'0'},
{id:4,已激活:'0'},
];
设b=[
{id:2,已激活:'1'},
{id:3,已激活:'1'},
{id:5,已激活:'0'},
];
让hash=a.flatMap(el=>el.id);
b、 forEach(elem=>{
index=hash.indexOf(elem.id);
如果(索引!=-1)
a[索引].已激活=元素已激活;
否则{
a、 推(elem);
hash.push(元素id);
}
});
控制台日志(a)代码>从数组a的ID构建哈希。通过在散列中查找索引b.id来迭代数组b,如果在a中找到索引b.id,则已激活已建立的索引属性。否则(在示例id:5中),旧数组中不存在该id的条目,因此创建一个新的条目并调整哈希大小
a=[
{id:1,已激活:'0'},
{id:2,已激活:'0'},
{id:3,已激活:'0'},
{id:4,已激活:'0'},
];
设b=[
{id:2,已激活:'1'},
{id:3,已激活:'1'},
{id:5,已激活:'0'},
];
让hash=a.flatMap(el=>el.id);
b、 forEach(elem=>{
index=hash.indexOf(elem.id);
如果(索引!=-1)
a[索引].已激活=元素已激活;
否则{
a、 推(elem);
hash.push(元素id);
}
let c = a.map( aObj => {
let found = b.find(bObj => bObj.id === aObj.id);
if(found) return found;
else return aObj;
});
let a = [
{ id: 1, activated: '0' },
{ id: 2, activated: '0' },
{ id: 3, activated: '0' },
{ id: 4, activated: '0' },
];
let b = [
{ id: 2, activated: '1' },
{ id: 3, activated: '1' },
];
const c = a.map(e => {
let newValue = b.find(n => e.id === n.id);
if (!!newValue) {
return newValue;
}
return e;
});