1

My goal is to remove properties in an object with values greater than a given number "num" using a function. However, "delete" is not working. What would make delete not work in this situation, and is there a better solution?

    function removeNumbersLargerThan(num, obj) {
      for(var key in obj){
        if(obj[key] > num){
          delete obj.key;
        }
      }
      return obj;
    }

    var obj = {
    a: 8,
    b: 2,
    c: 'montana'
    }
    removeNumbersLargerThan(5, obj);
    console.log(obj); // --> { b: 2, c: 'montana' }
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
tskittles
  • 126
  • 1
  • 2
  • 9

4 Answers4

0

you are almost done.use same like if condition obj key match .delete obj[key]

function removeNumbersLargerThan(num, obj) {
  for(var key in obj){
    if(obj[key] > num){
      delete obj[key];
    }
  }
  return obj;
}

var obj = {
a: 8,
b: 2,
c: 'montana'
}
removeNumbersLargerThan(5, obj)
console.log(obj); // --> { b: 2, c: 'montana' }
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

You're flipping what property you're accessing. You got it right the first time, but not the second:

function removeNumbersLargerThan(num, obj) {
  for(var key in obj){
    if(obj[key] > num){ // Correct!
      delete obj.key; // Should be obj[key] again
    }
  }
  return obj;
}

Otherwise you're going to delete the "key" property off of the obj


In other words, your loop isn't deleteing the property you actually want to delete. It's just deleteing the key property on an Object:

// What you declare
let num = 5;
let obj = {
    key: "hello",
    keyToKill: 7,
    keyToLeaveAlone: 3
}

// Your initial loop to remove keys with value greater than a number
for (let key in obj) {
    // If I find a key whose value is greater than num
    if (obj[key] > num) {
        // Delete the "key" property
        delete obj.key;
    }
}

// The above will give you this:
let incorrect = {
    keyToKill: 7,
    keyToLeaveAlone: 3
}

If you want to delete the property whose value is greater than num you would do the following:

// If you do the following instead:
for (let key in obj) {
    // If I find a key whose value is greater than num
    if (obj[key] > num) {
        // Delete the property who's value is greater than num
        delete obj[key];
    }
}

// You'll get this
let correct = {
    key: "hello",
    keyToLeaveAlone: 3
}
zero298
  • 25,467
  • 10
  • 75
  • 100
0

You need the right property accessor with bracket notation for using a variable as value.

delete obj[key];
//        ^   ^

function removeNumbersLargerThan(num, obj) {
    for (var key in obj) {
        if (obj[key] > num) {
            delete obj[key];
        }
    }
    return obj;
}

var obj = { a: 8, b: 2, c: 'montana' };

removeNumbersLargerThan(5, obj);
console.log(obj); // { b: 2, c: 'montana' }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

    function removeNumbersLargerThan(num, obj) {
      for(var key in obj){
        if(obj[key] > num){
          delete obj.key;
        }
      }
      return obj;
    }

    var obj = {
    a: 8,
    b: 2,
    c: 'montana'
    }
    removeNumbersLargerThan(5, obj);
    console.log(obj); // --> { b: 2, c: 'montana' }

  function removeNumbersLargerThan(num, obj) {
      for(var key in obj){
        if(obj[key] > num){
          delete obj[key];
        }
      }
      return obj;
    }

    var obj = {
    a: 8,
    b: 2,
    c: 'montana'
    }
    removeNumbersLargerThan(5, obj);
    console.log(obj); // --> { b: 2, c: 'montana' }