user3711357
user3711357

Reputation: 1625

How to empty an object (object fields can be vary and dynamic)

How to reset the object as below. As a static object, I can use the below one but, have this object can contain additional fields or less fields then below as its dynamically provided. How to make empty this object as below.

var object={
    id:0,
    clientId:0,
    position:'',
    positionCnt:'',
    InsValue:'',
        Code:'',
        FieldName:''
};

Upvotes: 0

Views: 686

Answers (5)

Tyler Roper
Tyler Roper

Reputation: 21672

You could set an object that defines the default values by type (string, number, etc).

With this defined, the below would loop through the object and set the property values to their type-defaults.

Important: The code below is a solid start but it doesn't handle arrays, nested objects, etc, so you'll have to flesh it out a bit.

var defaultTypeValues = {
  number: 0,
  string: '',
  boolean: false
};

var obj = {
  id: 525,
  clientId: 1624,
  position: 'pos',
  positionCnt: 'abc',
  InsValue: 'def',
  Code: 'abc123',
  FieldName: 'fieldname'
}

function emptyObj(obj, defaultOverrides = {}) {
  var defaults = Object.assign({}, defaultTypeValues, defaultOverrides);
  Object.keys(obj).forEach(k => obj[k] = defaults[typeof obj[k]]);
  return obj;
}

console.log(emptyObj(obj));

I've added a second (optional) parameter defaultOverrides, where you can specify values to override the defaults, ie, emptyObj(obj, {boolean: true}); would replace all bool values with true instead of the default false.

Upvotes: 2

ABC
ABC

Reputation: 2148

var a = {one: 1, two: '2', three: 3, four: true}

let sort = (obj, val, val2, val3) => { 
  Object.keys(obj).forEach(x => {
    if (typeof obj[x] === 'string') {
       obj[x] = val;
    } else if (typeof obj[x] === 'number') {
       obj[x] = val2;
    } else if (typeof obj[x] === 'boolean') {
       obj[x] = val3;
    }  else { 
       // Extend here to datatypes
    }
  });
}
let set = obj => sort(obj, '', 0, false); // Additional types

set(a)
console.log('After:', a)

Upvotes: 1

Ivan Burnaev
Ivan Burnaev

Reputation: 2730

Here is the example of working prototype with nested objects.
It doesn't use variables out of the scope, easily testable and maintainable.

const toDefault = defaults => {
  return function resetter(o) {
    Object.keys(o).forEach(key => {
      if (typeof o[key] === "object") {
        o[key] = resetter(o[key]);
      } else {
        o[key] = defaults[typeof o[key]];
      }
    })
    
    return o;
  }
}

const reset = toDefault({string: "", number: -1});

var obj = {
    id: 1230,
    clientId:0,
    position:'',
    positionCnt:'',
    InsValue:'',
    Code:'12312312',
    FieldName:'',
        
    nested: {
      a:12312,
      b:123123
    }
};

console.log(reset(obj));

Upvotes: 0

zfrisch
zfrisch

Reputation: 8670

You can use Object.assign

let newObject = Object.assign(object, {position: "x"});

var object={
    id:0,
    clientId:0,
    position:'',
    positionCnt:'',
    InsValue:'',
        Code:'',
        FieldName:''
};
let newObject = Object.assign(object, {position: "x"});
console.log(newObject);

Or the spread operator ...obj

    let newObject = { ...object, position: "x"};

var object = {
  id: 0,
  clientId: 0,
  position: '',
  positionCnt: '',
  InsValue: '',
  Code: '',
  FieldName: ''
};

let newObject = { ...object,
  position: "x"
};

console.log(newObject);

Upvotes: 0

Quentin
Quentin

Reputation: 944172

Keep a template object so you know what you want to reset it to.

Then loop over and erase all the properties, and assign the template over it.

const template = {
  id: 0,
  clientId: 0,
  position: '',
  positionCnt: '',
  InsValue: '',
  Code: '',
  FieldName: ''
};

let object = {
  id: 0,
  //  clientId: 0,
  //  position: '',
  positionCnt: '',
  InsValue: '',
  Code: '',
  FieldName: 'with a different value',
  extraField: "Hello, world"
};

console.log("Before", JSON.stringify(object, null, 2));

Object.keys(object).forEach(key => {
  delete object[key];
});

Object.assign(object, template);

console.log("After", JSON.stringify(object, null, 2));

Upvotes: 0

Related Questions