简体   繁体   中英

Add additional object keys into array based on condition javascript

My array:

[
  {
    name: 'test1',
    state: 'OK',
    status: true,
    pending: 33,
    approved: 0,
    active: 0,
    inactive: 33
  },
  {
    name: 'test3',
    state: 'OK',
    status: true,
    pending: 33,
    approved: 0,
    active: 0,
    inactive: 33
  },
  {
    name: 'test4',
    state: 'OK',
    status: true
  }
]

If the "pending", "approved", "active", "inactive" key not exists in object, i need output like this:

Expected output:

[
  {
    name: 'test1',
    state: 'OK',
    status: true,
    pending: 33,
    approved: 0,
    active: 0,
    inactive: 33
  },
  {
    name: 'test3',
    state: 'OK',
    status: true,
    pending: 33,
    approved: 0,
    active: 0,
    inactive: 33
  },
  {
    name: 'test4',
    state: 'OK',
    status: true,
    pending: 0,
    approved: 0,
    active: 0,
    inactive: 0
  }
]

How to do this?

I tried with map but i dont know how to set condition.

I want to set the values into zero.

You can use Array.map() and use an array of properties, iterate over the array of properties and check for each object if that property is present in the object or not, if it is not present than simply add the property and assign it value as 0.

 let arr = [ { name: 'test1', state: 'OK', status: true, pending: 33, approved: 0, active: 0, inactive: 33 }, { name: 'test3', state: 'OK', status: true, pending: 33, approved: 0, active: 0, inactive: 33 }, { name: 'test4', state: 'OK', status: true } ]; let props = ['active','inactive', 'approved', 'pending']; let result = arr.map(a =>{ props.forEach(prop=> a[prop] = a[prop] || 0); return a; }); console.log(result); 

You can use .forEach to apply your condition to each object.

 arr = [ { name: 'test1', state: 'OK', status: true, pending: 33, approved: 0, active: 0, inactive: 33 }, { name: 'test3', state: 'OK', status: true, pending: 33, approved: 0, active: 0, inactive: 33 }, { name: 'test4', state: 'OK', status: true } ] arr.forEach(obj => {for (let p of ['pending', 'approved', 'active', 'inactive']){ if (!obj.hasOwnProperty(p)){ obj[p] = 0; } }}); console.log(arr); 

  • Create an object having properties with their default values.
  • Use .map() to iterate over objects of the given array by passing a callback.
  • Use Object.assign() method to create a close of the current object by passing an empty object, default object and current object as arguments. First defaults values will be copied into empty object and then Object.assign() will copy each property from the current object in the cloned effectively overriding the default values.

Below is a demo:

 let data = [ {name: 'test1',state:'OK',status:true,pending: 33,approved: 0,active: 0,inactive: 33}, {name: 'test3',state:'OK',status:true,pending: 33,approved: 0,active: 0,inactive: 33}, {name: 'test4',state:'OK',status:true} ]; let defaults = { pending: 0, approved: 0, inactive: 0, active: 0 }; let result = data.map(o => Object.assign({}, defaults, o)); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Resources:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM