Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove property in Javascript [duplicate]

Tags:

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?

like image 642
casillas Avatar asked May 14 '15 23:05

casillas


People also ask

How do you remove duplicate objects?

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.

How can you eliminate duplicate values from a JavaScript 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.


2 Answers

You can delete it using delete

delete b[0].color 
like image 133
Menztrual Avatar answered Oct 22 '22 15:10

Menztrual


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; } 
like image 39
Edwin Reynoso Avatar answered Oct 22 '22 15:10

Edwin Reynoso