0

I tried to push into an array I just created within an object using function but it keeps returning numbers instead of the value pushed.

  const recordCollection ={
    5439: {
      albumTitle: 'ABBA Gold'
    }
  }
  function updateRecords(records, id, value){
    records[id].track = [];
    return  records[id].track.push(value)
  }
  console.log(updateRecords(recordCollection, 5439,'ABBA'));

according to my code, my result is supposed to be 'ABBA' but it keeps returning the size of the array instead

image of what i got

  • per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push, "The `push()` method adds one or more elements to the end of an array and returns the new length of the array." You have `return records[id].track.push(value);`, which is returning the new length of the array. Instead, you want to do `records[id].track.push(value); return value;` – WOUNDEDStevenJones Mar 30 '22 at 14:55

0 Answers0