【发布时间】:2019-04-24 07:40:53
【问题描述】:
我想知道如何在javascript中通过id=insta为嵌套对象中的对象属性赋值
我有两个对象,我需要使用 javascript 将一个对象属性应用到另一个对象
我被卡住了,不知道如何继续,
obj1.forEach(e=> {if(e.id==='insta') Object.assign(e, obj2)})
var obj1 = [
{
id: "insta",
rate: "2.4",
fee: "0",
amount: "400"
},
{
id: "trans",
rate: "1.4",
fee: "0",
amount: "200"
}
]
var obj2 =
{
data: {
rate_value: "4.4",
fee_value: "10",
targetamount: "5000",
country_code: "SG"
}
}
Expected Output:
res= [
{
id: "insta",
rate: "4.4",
fee: "10",
amount: "5000",
country_code: "SG"
}
]
【问题讨论】:
-
您希望 obj2 的所有属性都在 res 对象中还是仅在 country_code 中?
-
如何映射属性?这看起来不像是一个合理的标准。
rate_value变为rate,而targetamount变为amount?
标签: javascript jquery arrays html object