3
var object = {
   'ex1': '123',
   'ex2': '234',
   'ex3': '345',
   'ex4': '456',
   'ex5': '678'
}

What is the best way to remove values without remove property as follows?

var object = {
   'ex1': '',
   'ex2': '',
   'ex3': '',
   'ex4': '',
   'ex5': ''
}
1
  • 2
    for (const p in obj) obj[p] = '';? Commented Nov 6, 2018 at 7:54

5 Answers 5

3

You could get the keys, iterate and assign a wanted value to the properties.

var object = { ex1: '123', ex2: '234', ex3: '345', ex4: '456', ex5: '678' };

Object
    .keys(object)
    .forEach(k => object[k] = '');

console.log(object);

Sign up to request clarification or add additional context in comments.

Comments

3

Simply use for...in to iterate over the object properties:

var object = {
   'ex1': '123',
   'ex2': '234',
   'ex3': '345',
   'ex4': '456',
   'ex5': '678'
}

for (const prop in object) { object[prop] = '' };

console.log(object);

Comments

2

Iterate though the object keys using Object.keys and set the values to empty. Also you need to name you object something other than Object since its already a predefined object in javascript.

var object = {
   'ex1': '123',
   'ex2': '234',
   'ex3': '345',
   'ex4': '456',
   'ex5': '678'
}

Object.keys(object).forEach(key => {
   object[key]  = ''
})
console.log(object);

2 Comments

object is not a good name for a variable, but it's perfectly legal.
@connexo It does prevent us from using Object.keys though :-)
0

Also, you can reach it by using the reduce function.

var obj = {
   'ex1': '123',
   'ex2': '234',
   'ex3': '345',
   'ex4': '456',
   'ex5': '678'
}

const new_object = Object.keys(obj).reduce((agg, item) => {
  agg[item] = ''
  return agg
}, {})

console.log(new_object)

The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.

EDIT:

Notice that reduce will return an new object (It will not override the existing object).

If you prefer to override the existing object, look at other answers who use the forEach loop.

3 Comments

This would not change references to the element.
Although it definitely works it creates a new object which might be desired but might not also be what the author wanted
Thanks, I should have mentioned this, I've added an Edit section.
0

I think there are all many approaches.

I like this one:

    const obj = {
      ex1: '123',
      ex2: '234',
      ex3: '345',
      ex4: '456',
      ex5: '678'
    }

    for (const p in obj) obj[p] = null;

    console.log(obj);

But I find also acceptable the one using ES6 iterators:

    const obj1 = {
      ex1: '123',
      ex2: '234',
      ex3: '345',
      ex4: '456',
      ex5: '678'
    }

    for (k of Object.getOwnPropertyNames(obj1)) {
      obj1[k] = null;
    }

    console.log(obj1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.