Remove a property from an object contained in an array

8

I have a variable array:

Bola = [];

I've added properties to it:

Bola[0] = { peso:0.5, cor:"branca", marca:"nike", nota:8 };
Bola[1] = { peso:0.7, cor:"verde", marca:"adidas", nota:9  };

I would like to remove the "mark" and "weight" properties. How do I do this?

    
asked by anonymous 24.03.2015 / 15:47

1 answer

10

You can use delete to remove properties from objects inside the array (which is what you have, two objects inside the array bola .

// Não esqueça o var!
var bola = [];
bola[0] = { peso:0.5, cor:"branca", marca:"nike", nota:8 };
bola[1] = { peso:0.7, cor:"verde", marca:"adidas", nota:9  };

for(var i=0; i<bola.length; i++) {
    delete bola[i].marca;
    delete bola[i].peso;
}
document.body.innerHTML = JSON.stringify(bola);

Note: Use lowercase letters to name variables, leave the initials capitalized for "classes" (constructor functions).

    
24.03.2015 / 15:50