Home  >  Q&A  >  body text

How to combine two arrays of objects into a single array and object using javascript

I want to know how to merge two arrays of objects into a single array using javascript

var arr1 = [
  {编号:1, name: "xxx"}
]

var arr2 =[
 {详细信息:“财务”, 费用:“100”}
]

Tried.
var result1 = [...arr1, ...arr2];
var result=Object.assign({}, ...result1)

Expected output [ { Number: 1, Name: "xxx", Details: "Finance", Cost: "100" } ]

P粉254077747P粉254077747177 days ago1457

reply all(1)I'll reply

  • P粉955063662

    P粉9550636622024-04-06 09:21:57

    var arr1 = [
      {id:1, name: "xxx"}
    ]
    
    var arr2 =[
     {details:"finance", cost:"100"}
    ]
    
    const result = arr1.map((item, index) => ({ ...item, ...arr2[index] }))
    
    console.log(result)

    reply
    0
  • Cancelreply