I am a newbie here so my question may sound stupid.
I have an array with multiple objects and I am not sure how to push the key name of each object to an array.
This is my code:
var ingredients = [
{ id: 1, name: "onion", mineralsUnit: "mg", water: 89.11 },
{ id: 2, name: "carrot", calcium: 23, iron: 0.21, otherUnit: "g", water: 89.11 },
{ id: 3, name: "peanut", iron: 0.21, otherUnit: "g", water: 89.11 }
];
and I've created a new empty array where I want to add just the name from the first array
var myIngredients = [];
I've tried that:
for (var i = 0; i < ingredients.length; i++) {
myIngredients.push(ingredients[i]);
}
but it returns the entire array and even though I select in here ingredients[I] what element I want to pick it's still not working. If someone has an idea I would really appreciate it. Thanks a lot.