Lex V
Lex V

Reputation: 1447

Join an array with an object in Reactjs

I having an array

let dd =[0: {name:'clarie', dob:15/12/1990}]

and an object

let info ={state:'washigton',zip:'54665'}

I want to merge this 2 into like,

  let y = {name:'clarie', dob:15/12/1990,state:'washigton',zip:'54665'}

I used spread opertor,concat, but error occurs.

let y= [..dd[0], ..info]  like  this

How to join these two?

Upvotes: 0

Views: 74

Answers (2)

NeERAJ TK
NeERAJ TK

Reputation: 2695

Your current code spreads info object with dd array. You can't spread object properties onto an array. You should spread it as an object, as :

     const y = { ...info, ...dd[0] }

Also, the array initialization is wrong in the question, dob should be string. Updated code:

  const dd = [{ name: "clarie", dob: "15/12/1990" }];
  const info = { state: "washigton", zip: "54665" };
  const y = { ...info, ...dd[0] }

View demo on codesandbox

Upvotes: 2

Elley
Elley

Reputation: 950

let y= {...(dd[0]), ...info}

You want to create a new object, not an array.

Upvotes: 1

Related Questions