Try to search your question here, if you can't find : Ask Any Question Now ?

How to push an object with two key/value pairs into an array?

HomeCategory: stackoverflowHow to push an object with two key/value pairs into an array?
Avatarcraig asked 1 week ago

I want to take this array as an argument

var movies = ['matrix','the dark knight','a beautiful mind','american pie']

for a function that loops through that array and pushes the titles as object into a new array adding an id (from the loop index) along the way.

The new arr should look like this

['{title:matrix, id:0}','{title:the dark knight, id:1}','{title:a beautiful mind, id:2}','{title:american pie, id:3}']

This is the function and I know I have a syntax error. But where?

function addToList(arr) {
    var movieList = []

    for (var x of arr) {
        movieList{["titel"]=arr, ["id"]=x}
    }
    return movieList
}
1 Answers
Best Answer
AvatarArben answered 1 week ago
Your Answer

4 + 15 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com