jessieWJ
jessieWJ

Reputation: 53

How could i get all values out?

Here is the assignment I have. Declare a function getAllValues. You may need to use both kinds of for loops!

/**
* @param {Array} ???
* @returns {Array} an array of all of the values in all of the given objects
*/
function getAllValues(arrayOfObject){ 
    let arr = [];
    for(const e of arrayOfObject){
        let obj = arrayOfObject[e];
        for(const key in obj){ 
            arr.push(obj[key]) ;
        } 
    }
    return arr;
}

// test
test(getAllValues(collection), [1, 2, 3, 3, 4, 100]); // returns []
test(getAllValues(collection.slice(1)), [3, 3, 4, 100]); // returns []

What's wrong with my current code?

Upvotes: 1

Views: 106

Answers (1)

Tanay
Tanay

Reputation: 889

Looks like the error is here:

let obj = arrayOfObject[e];  

Here the variable e itself is the object from the array, not an index. So it should be:

let obj = e;

Upvotes: 1

Related Questions