0

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?

1
  • 1
    let dd =[0: {name:'clarie', dob:15/12/1990}] is not an valid array, please update the question Commented Apr 6, 2022 at 11:03

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

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

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

4 Comments

I am not saying I want to create an new array
"let y= [..dd[0], ..info] like this" this would be something to create an array :)
oo yaa I typed wrong.. Sorry
No problems :) So, is your issue solved? Or does is still not work ? If it's solved, you should select a good answer to close the issue :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.