gpt4 book ai didi

javascript - 使用 es6 将对象的特定 Prop 合并为一个?

转载 作者:行者123 更新时间:2023-11-30 08:23:54 25 4
gpt4 key购买 nike

这是迄今为止在 SO 中提出的最常见的问题,但是,总的来说,提出的问题是指合并两个完整的对象。

就我而言,情况完全不同。

假设我得到:

const P1 = {
"name" : "Person1",
"profession" : "Student",
"gender" : "Male",
"type" : "Patient",
}

const E1 = {
"age" : "30",
"dob" : "20/12/1970",
"address" : "122 Harrow Street",
"contactNumber" : "07473033312",
}

我想合并这两个给我以下内容:

const Result = {
"name" : "Person1",
"type" : "Patient",
"age" : "30",
"dob" : "20/12/1970",
}

问题是,我不想合并两个完整的项目。我想在不循环的情况下合并特定 Prop 。

目前,我们可以像这样使用spread来实现合并:

常量数据 = [...P1, ...E1];

但这合并了两者,这是我不想要的。

最佳答案

 const result = (({name, type}, {age, dob}) => ({name, type, age, dob}))(P1, P2);

只是部分破坏 P1 和 P2 并构建一个新对象。

关于javascript - 使用 es6 将对象的特定 Prop 合并为一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48795653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com