The problem of merging two or more objects in JavaScript

CSDN Q & A 2022-02-13 05:10:51 阅读数:347

problem merging objects javascript

data1 = {
title: ' income -1'
,id: '1001'
}

data2 = {
title: ' income -2'
,id: '1002'
}

Want to spell it like this
data3={ {
title: ' income -1'
,id: '1001'
},{
title: ' income -2'
,id: '1002'
}}

Who can answer , thank




Take the answer :

Plug in one click merge transformation to understand



Other answers 2:
data3={
{title: ' income -1',id: '1001'},{title: ' income -2',id: '1002'}}

It says , Grammar doesn't make sense , You can turn it into an array

data3 = [ { title: ' income -1', id: '1001', }, { title: ' income -2', id: '1002', },]

Or if you have to have an object , You can write like this

data3 = { data1: { title: ' income -1', id: '1001', }, data2: { title: ' income -2', id: '1002', },}

If it is hierarchical , You can write like this , Easy recursion

data3 = { title: ' income -1', id: '1001', children: [ { title: ' income -1', id: '1001', children: [], }, { title: ' income -2', id: '1002', children: [], }, ],}

Other answers 3:

img

var data1 = {title: ' income -1',id: '1001'}var data2 = {title: ' income -2',id: '1002'}var list = [];var list1 = [];list1.push(data1)var list2 = [];list2.push(data2);list.push(data1);list.push(data2);var listobj = {}; for (var key in list) { listobj [key] = list[key]; } console.log(listobj );

Other answers 4:
 var arr = [{ title: ' income -1', id: '1001' }, { title: ' income -2', id: '1002' }] var data3 = Object.assign({}, arr) console.log(data3)
copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130510494257.html