0

I've tried to spread my array of object to create a new object but instead destroying the previous object

this is how I tried to spread my array

const currentArraySlug = [
  {
    slug: "jagung-com", content_viewed: [1, 2], all_viewed: false
  }
]

const newSlug = {
  slug: "New slug",
  content_viewed: [],
  all_viewed: false
}

const mergeData = Object.assign({}, newSlug, ...(currentArraySlug))

console.log(mergeData);

This is my Expected output

[
  {
    slug: "jagung-com",
    content_viewed: [1, 2],
    all_viewed: false
  },
  {
    slug: "New slug",
    content_viewed: [],
    all_viewed: false
  }
]

How can I deal with it, Thanks in advance

1
  • 3
    const mergeData = currentArraySlug.concat(newSlug) ?? Commented Apr 30, 2021 at 2:10

1 Answer 1

1

Another way to merge data with spread operation is as below

const currentArraySlug = [
  {
    slug: "jagung-com", content_viewed: [1, 2], all_viewed: false
  }
]

const newSlug = {
  slug: "New slug",
  content_viewed: [],
  all_viewed: false
}

const mergeData = [newSlug, ...currentArraySlug];

console.log(mergeData);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.