Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Create an Array of Objects

Gal Parselany
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Gal Parselany
Full Stack JavaScript Techdegree Graduate 26,281 Points

my solution :

let products = [
    {
        name : 'tuna',
        inventory : 4,
        unit_price : 8.60
    },

    {
        name : 'Salmon',
        inventory : 10,
        unit_price : 5.60
    },
];

function listProducts(obj) {
    let objName = [];
    for (i = 0; i < obj.length; i++) {
        let indexName = obj[i].name;
        objName.push(indexName);
    }
    console.log(objName);
    return objName;
}

listProducts(products);

function totalValue(arr) {
    let totalVal = 0;
    for (i = 0; i < arr.length; i++) {
        arrVal = arr[i].inventory * arr[i].unit_price;
        totalVal += arrVal;
    }
    console.log(totalVal);
    return totalVal
}

totalValue(products);
Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,708 Points

Added syntax hinting and removed your duplicate posts.