合并两个对象但保留子对象

时间:2021-06-17 22:03:21

标签: javascript

我一直在尝试合并两个对象,但保留它们的子对象,spreadObject.assign 似乎没有这样做。我发现他们替换了孩子并删除了那些不匹配的孩子。

如果我的意思不够清楚,请在下面找到我的意思,我还没有找到解决方案,也许来自我的解释。

const firstObject = {
    'united-states': {
        cities: {
            'new-york': {
                description: 'XXXX',
                image: 'URL 1',
            },
            boston: {
                description: 'XXXX',
                image: 'URL 1',
            }
        }
    },
    england: {
        cities: {
            london: {
                description: 'XXXX',
                image: 'URL 1',
            },
            manchester: {
                description: 'XXXX',
                image: 'URL 1',
            }
        }
    },
    france: {
        cities: {
            paris: {
                description: 'XXXX',
                image: 'URL 1',
            },
            lyon: {
                description: 'XXXX',
                image: 'URL 1',
            }
        }
    }
}

const secondObject = {
    'united-states': {
        cities: {
            boston: {
                description: 'YYYY',
                image: 'URL 2',
            }
        }
    },
    england: {
        cities: {
            london: {
                description: 'YYYY',
                image: 'URL 2',
            }
        }
    }
}

const mergedObject = {
    'united-states': {
        cities: {
            'new-york': {
                description: 'XXXX',
                image: 'URL 1',
            },
            boston: {
                description: 'YYYY',
                image: 'URL 2',
            }
        }
    },
    england: {
        cities: {
            london: {
                description: 'YYYY',
                image: 'URL 2',
            },
            manchester: {
                description: 'XXXX',
                image: 'URL 1',
            }
        }
    },
    france: {
        cities: {
            paris: {
                description: 'XXXX',
                image: 'URL 1',
            },
            lyon: {
                description: 'XXXX',
                image: 'URL 1',
            }
        }
    }
}

非常感谢!

尼古拉斯 V.H.

1 个答案:

答案 0 :(得分:0)

它只是传播运算符,但您在 cities 属性上使用它,而不是国家/地区。请注意,任何存在于 secondObject 但不存在于 firstObject 的国家都不会被计算在内。不过这很简单

const firstObject = {
  'united-states': {
    cities: {
      'new-york': {
        description: 'XXXX',
        image: 'URL 1',
      },
      'boston': {
        description: 'XXXX',
        image: 'URL 1',
      }
    }
  },
  england: {
    cities: {
      'london': {
        description: 'XXXX',
        image: 'URL 1',
      },
      'manchester': {
        description: 'XXXX',
        image: 'URL 1',
      }
    }
  },
  france: {
    cities: {
      'paris': {
        description: 'XXXX',
        image: 'URL 1',
      },
      'lyon': {
        description: 'XXXX',
        image: 'URL 1',
      }
    }
  }
}

const secondObject = {
  'united-states': {
    cities: {
      'boston': {
        description: 'YYYY',
        image: 'URL 2',
      }
    }
  },
  england: {
    cities: {
      'london': {
        description: 'YYYY',
        image: 'URL 2',
      }
    }
  }
}
 
let mergedObject = {}
for (let country in firstObject) {
let a=firstObject[country].cities, b=secondObject[country] && secondObject[country].cities || {};
   mergedObject[country] = {
    cities: { ...a,b}
  }
}

console.log(mergedObject)