-4

Having issues with array data

OUT PUTTING LIKE THIS
 [{
     "createdAt": [Object],
     "date": "2021-08-22",
     "description": "Yes"
   }, {
     "title": "greatt"
   }

I need it to output like this below

    [{
   "createdAt": [Object],
   "date": "2021-08-22",
   "description": "Yes",
   "title": "greatt"
 }
const myList = [];
myList.push(otherArray, {title: 'Hello'})
Jerry Seigle
  • 417
  • 4
  • 12

2 Answers2

0

The result you're getting indicates that otherArray is not an array, it's a single object

{
    "createdAt": [Object],
    "date": "2021-08-22",
    "description": "Yes"
}

So you're pushing two separate objects onto myList.

If you want to merge an object into another object, you can use Object.assign().

Object.assign(otherArray, {title: 'Hello'});

Then you can do

myList.push(otherArray);

to add that object to the myList array.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

I think what you want to do is push the extra object values inside the last object of the array ! So here's the solution for that =>

let array = [{
    "createdAt": [Object],
    "date": "2021-08-22",
    "description": "Yes"
  }]

  function push(props , array) {
    let lastIndex = array.length - 1;
    array[lastIndex] = Object.assign(props, array[lastIndex]);
  }

  push({
    title: "Title"
  }, array);

  console.log(array)

For more information about Object.assign checkout MDN

Sanmeet
  • 1,191
  • 1
  • 7
  • 15