Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 889
Mohamed Sameer Avatar asked Sep 01 '18 07:09

Mohamed Sameer


3 Answers

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);
like image 126
amrender singh Avatar answered Oct 18 '22 05:10

amrender singh


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);
like image 32
Joe Iddon Avatar answered Oct 18 '22 05:10

Joe Iddon


  • 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:

  • Array.prototype.map()
  • Object.assign()
like image 1
Mohammad Usman Avatar answered Oct 18 '22 05:10

Mohammad Usman