I have the following object
b.push({ data: d, width: 5, color: color });
then I have
b= [{data:10,width:5, color:"yellow"},{data:12,width:5, color:"red"},etc...];
I added color
property and now I do not need and I want to remove it. I would like to know what is easiest way to do it?
To remove the duplicates from an array of objects:Create an empty array that will store the unique object IDs. Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.
Answer: Use the indexOf() Method You can use the indexOf() method in conjugation with the push() remove the duplicate values from an array or get all unique values from an array in JavaScript.
You can delete it using delete
delete b[0].color
If you're using .push({})
and pushing an object literal and not having reference any other way to those objects just use map:
b = b.map(function(obj) { return {data: obj.data, width: obj.width}; });
If you happen to have reference then the only way I can really think of it would be to use the delete
keyword even though I don't recommend it:
for(var obj of b) { delete obj.color; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With