How do i add object to array? And after that values and properties to it?

How do i add object to array? And after that values and properties to it?
0

#1

So im trying to make a movie collection by my self, and im lost at adding object to array and later adding values with properties to new added array at the same time. Help would be appreciated. Keep in mind that code isnt finnished yet especially if statement that i want it to add object with values and properties with so im lost in that one, i’ll be working on it for a while since im practising with this too.

var movieCollection = [
    {
        name: "Spiderman",
        year: 1979
    },

    {
        name: "Batman",
        year: 1986
    },

    {
        name: "IT",
        year: 2017
    }
]

var input = prompt("Choose the action");

 while (input !== "quit") {
    if (input === "add") {
        var question = prompt("What do you wish to add?");
        var a = {};
        var b = movieCollection.push(a);
        
    } else if (input === "list") {
        movieCollection.forEach(function (movie) {
            console.log(movie);
        })
    }

    var input = prompt("Choose the action");
}
console.log("You exited");

#2

If I had the following array of movie objects:

var movieCollection = [
    {
        name: "Spiderman",
        year: 1979
    },

    {
        name: "Batman",
        year: 1986
    },

    {
        name: "IT",
        year: 2017
    }
]

and I want to add a new object with a name property values of ‘Jaws’ and year property value of 1975, I could write:

movieCollection.push({name: 'Jaws', year: 1975});

#3

You can add an object to an array using .push().

let arr = [1, 2, 3]
arr.push{4}

would end up with arr being [1,2,3,4].

You can access each element in an array by its index - [1,2,3,4][0] gets you the first element, which is 1.

If your elements have properties, you can affect them like this.

movieCollection[0].name = "Batman"

would change the name property of the first element in movieCollection.