Led*_*Led 2 javascript react-native
我有两个对象:
a = {id: "1", value: "hello"}
b = {id: "2", value: "bye"}
Run Code Online (Sandbox Code Playgroud)
我要转换为:
c = { 1: "hello", 2: "bye" }
Run Code Online (Sandbox Code Playgroud)
因此,创建一个没有任何嵌套主题的新对象.
我尝试了几种方法,但没有得到我需要的东西.如:
Object.keys(a).forEach((key) => { a[key] = b[key]; });
Run Code Online (Sandbox Code Playgroud)
你可以使用.reduce:
a = {id: "1", value: "hello"}
b = {id: "2", value: "bye"}
c = [a, b].reduce((carry, current) => { carry[current.id] = current.value; return carry }, {})
console.log(c)Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
90 次 |
| 最近记录: |