根据条件javascript将其他对象键添加到数组中

时间:2022-09-25 08:40:54

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:

如果对象中不存在“pending”,“approved”,“active”,“inactive”键,我需要输出如下:

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.

我想将值设置为零。

3 个解决方案

#1


3  

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.

您可以使用Array.map()并使用属性数组,遍历属性数组并检查每个对象(如果该属性存在于对象中),如果它不存在,则只需添加属性并分配它值为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);

#2


2  

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

您可以使用.forEach将条件应用于每个对象。

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);

#3


1  

  • Create an object having properties with their default values.
  • 创建具有默认值属性的对象。

  • Use .map() to iterate over objects of the given array by passing a callback.
  • 使用.map()通过传递回调来迭代给定数组的对象。

  • 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.
  • 使用Object.assign()方法通过传递空对象,默认对象和当前对象作为参数来创建当前对象的关闭。首先将默认值复制到空对象中,然后Object.assign()将复制克隆中当前对象的每个属性,从而有效地覆盖默认值。

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:

#1


3  

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.

您可以使用Array.map()并使用属性数组,遍历属性数组并检查每个对象(如果该属性存在于对象中),如果它不存在,则只需添加属性并分配它值为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);

#2


2  

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

您可以使用.forEach将条件应用于每个对象。

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);

#3


1  

  • Create an object having properties with their default values.
  • 创建具有默认值属性的对象。

  • Use .map() to iterate over objects of the given array by passing a callback.
  • 使用.map()通过传递回调来迭代给定数组的对象。

  • 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.
  • 使用Object.assign()方法通过传递空对象,默认对象和当前对象作为参数来创建当前对象的关闭。首先将默认值复制到空对象中,然后Object.assign()将复制克隆中当前对象的每个属性,从而有效地覆盖默认值。

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: