Pushing new objects into an empty array with a function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Pushing new objects into an empty array with a function

So i'm working on a project. A system to handle data for a record store. //say i have the variable set to an empty array let collection = [] //Then a function called function addToCollection( title, artist, year){ } // that when called on, creates a new object called record and pushes it into the array. I can't get the syntax correct to return the array with the record object in it correct via console.log //HELP PLEASE

13th Apr 2019, 5:39 PM
Cory
Cory - avatar
2 Answers
+ 3
Do you issues with pushing objects to the array? collection.push(new record())?? And actually console.log won't print the object properties, which you might have expected, on its own by just writing something like console.log(collection) Rather it'll log something like [object, object, object,...]. To check objects properties, yiu need to log them explicitly.
13th Apr 2019, 5:53 PM
Шащи Ранжан
Шащи Ранжан - avatar
0
Use this function function addToCollection( title, artist, year){ collection.push({title, artist, year}); } https://code.sololearn.com/W6nM358Addm9/?ref=app
14th Apr 2019, 2:43 AM
Calviղ
Calviղ - avatar