27

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?

1

3 Answers 3

12

You can delete it using delete

delete b[0].color
Sign up to request clarification or add additional context in comments.

9 Comments

Bu i have an array of objects pushed in the b.
@ilyasUyanik: So use a loop...?
Provide a better dataset @ilyasUyanik...
@ilyasUyanik: There isn't. Use forEach or a for loop.
@ilyasUyanik: A loop is the built-in method to process a list of item.
|
1

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;
}

8 Comments

Prefer a normal for loop to iterate over an array.
Well depends if you're trying to suppor IE then perhaps, but a modern browser I'd use for-of
Believe it or not, I missed the of and thought it was a for/in loop. I agree, for the future, for/of is probably the way to go (even though I don't think it can achieve the performance of a for loop).
@FelixKling you do know for of is new, it's part of ES6 and it's not just for arrays. It's for anything that's iterable, arrays, strings, maps, sets anything with a [Symbol.iterator] correctly implemented on. 2ality.com/2013/06/iterators-generators.html
and V8 optimizes for-of also there's was some news I don't remember where there's an article that now for(var i = 0; i < arr.length; i++) {} and for(var i = 0, l = arr.length; i < l; i++) {} are both just as fast.
|
1

You can write a function to find all objects where they have a property and that property has a targeted values that you want to delete.\

The program is pretty self-explanatory. Add a comment if you are missing a concept.

/* Redirect console output to HTML. */ document.body.innerHTML = '';
console.log=function(){document.body.innerHTML+=[].slice.apply(arguments).join(' ')+'\n';};

var b = [{
  data: 'Red',
  width: 1,
  color: '#FF0000'
}, {
  data: 'Blue',
  width : 1,
  color: '#00FF00'
}, {
  data: 'Green',
  width: 1,
  color: '#0000FF'
}];

function removeProperty(items, key, value, propToRemove) {
  items.forEach(function(item) {
    if (item != null && item[key] === value) {
      delete item[propToRemove];
    }
  });
}

// delete the 'color' property of the provided data matches.
removeProperty(b, 'data', 'Blue', 'color');

console.log(JSON.stringify(b, null, '  '));
body { font-family: monospace; white-space: pre; font-size: 11px; }

3 Comments

Why the != null bit? This way you can't match falsey values. In fact why test in the first place, I thought OP wanted to remove props from all objects.
It tests whether the object even has the property. It is similar to item.hasOwnProperty('key').
Similar, but not the same. What's the reason to have it in the first place?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.