Skip to content Skip to sidebar Skip to footer

Iterative Conditional Removal Of Object Property Using 'for..in' And 'if'

The function should remove any property that meets the 'if' condition inside the 'for' loop, but it doesn't.

Solution 1:

You miss the square brackets, while defining the object key to delete.

functionremoveNumbersLargerThan(num, obj) {
  for (var key in obj) {
    if (!isNaN(obj[key]) && obj[key] > num) {
      delete obj[key];
    }
  }
  return obj;
}
var obj = {
  a: 8,
  b: 2,
  c: 'montana'
}
removeNumbersLargerThan(5, obj);
console.log(obj); // Should be {b: 2, c: 'montana'}

Solution 2:

You should replace delete obj.key; with delete obj[key];

Post a Comment for "Iterative Conditional Removal Of Object Property Using 'for..in' And 'if'"