代码之家  ›  专栏  ›  技术社区  ›  Sameer

基于条件javascript向数组中添加其他对象键

  •  4
  • Sameer  · 技术社区  · 8 年前

    我的阵列:

    [
      {
        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”、“approved”、“active”、“inactive”键,则需要如下输出:

    预期产量:

    [
      {
        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
      }
    ]
    

    怎么做?

    我试过地图,但我不知道如何设置条件。

    我想把值设为零。

    3 回复  |  直到 8 年前
        1
  •  3
  •   amrender singh    8 年前

    你可以用 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
  •   Joe Iddon    8 年前

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

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

    资源: