【问题标题】:Merging 2 objects into a new one, with selective condition将 2 个对象合并为一个新对象,具有选择性条件
【发布时间】:2021-09-06 00:46:42
【问题描述】:

我正在尝试根据条件创建一组新的对象,将新组件从第二个组件添加到第一个组件。这是一个例子:

拥有这 2 组对象:

const one = [{code: 'a'},{ code: 'b'}, {code: 'c'}]
const two = [{code: 'b', status: 'active'}]

并希望得到如下结果:

{ code: 'a', status: 'empty' }
{ code: 'b', status: 'active' }
{ code: 'c', status: 'empty' }

这是我编写的示例代码:

const one = [{code: 'a'},{ code: 'b'}, {code: 'c'}]
const two = [{code: 'b', status: 'active'}]



var final = []
var temp = {}

for(let holiday of one) {
    // here I started to create a temp object with components of object one
    temp.code = holiday.code
    
    // here I check if same code from object one exist in any object two
    var checksub= two.find(o => o.code == holiday.code)

    // then assigns proper value to temp object from object two, of a default instead
    if(checksub){temp.status = checksub.status}
    else {temp.status = 'empty'}

    // then push the object into new array of objects
    final.push(temp)
}
    
console.log(final)

但我得到的结果是:

[
  { code: 'c', status: 'empty' },
  { code: 'c', status: 'empty' },
  { code: 'c', status: 'empty' }
]

为什么我得到最后一个对象的 3 倍?我尝试了不同的方法(使用 forEach 和 for 循环)并且总是得到同样奇怪的结果......我做错了什么? 谢谢!!!

【问题讨论】:

  • 将这个var temp = {}移到for循环中。

标签: node.js arrays json object


【解决方案1】:

您通过引用传递对象,因此如果您在一个位置更改对象的值,则使用该值的所有位置都将被更新。由于在每个循环中, temp 的值都会更改,因此您的数组会使用更新后的值进行更新。因此,如果您只是传递临时对象的克隆,那么您的所有值都不会受到影响。

const one = [{code: 'a'},{ code: 'b'}, {code: 'c'}]
const two = [{code: 'b', status: 'active'}]



var final = []
var temp = {}

for(let holiday of one) {
    // here I started to create a temp object with components of object one
    temp.code = holiday.code
    
    // here I check if same code from object one exist in any object two
    var checksub= two.find(o => o.code == holiday.code)

    // then assigns proper value to temp object from object two, of a default instead
    if(checksub){temp.status = checksub.status}
    else {temp.status = 'empty'}

    // then push the clone of object into new array of objects
    final.push({...temp})
}
    
console.log(final)

您所要做的就是final.push({...temp}),它将解构临时对象并将其作为新对象传递。

决赛如下:

[
 {code: "a", status: "empty"},
 {code: "b", status: "active"},
 {code: "c", status: "empty"},
]

【讨论】:

  • 欢迎 :) 您可以选择此答案作为正确答案,以便其他寻找类似内容的人可以立即找到答案
【解决方案2】:
const one = [{code: 'a'},{ code: 'b'}, {code: 'c'}]
const two = [{code: 'b', status: 'active'}]

const final = one.map((x) => {
    const twoContains = two.find(y => y.code === x.code);
    return {
        code: x.code,
        status: twoContains ? twoContains.status : 'empty'
    };
});

或简单(更短的版本)

const final = one.map(x => ({
    code: x.code,
    status: (two.find(y => y.code === x.code) || {status: 'empty'}).status
}));

console.log(final);

/* output
[
    { code: 'a', status: 'empty' },
    { code: 'b', status: 'active' },
    { code: 'c', status: 'empty' }
]    
*/

就这么简单

【讨论】:

    猜你喜欢
    • 2018-02-09
    • 2017-09-22
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    相关资源
    最近更新 更多