【问题标题】:javascript extract certain properties from all objects in arrayjavascript从数组中的所有对象中提取某些属性
【发布时间】:2019-02-08 17:21:07
【问题描述】:

我有一组具有相同属性的对象。每个对象都有大约一百个属性。我只想将其中的少数保留在一个新数组中:

var dummyArray = [{ "att1": "something", "att2": "something", ..., "att100": "something"}, { "att1": "something", "att2": "something", ..., "att100": "something"}, ...];

如何过滤/映射/减少...并提取有趣的键?

const newDummArray = dummyArray.map(function(item) { 
    delete item.att1; 
    delete item.att3; 
    delete item.att15;
    // ... (long list)
    return item; 
});

我怎样才能为每个对象只保留att20att30att70att80 而删除其余对象?

【问题讨论】:

    标签: javascript arrays ecmascript-6


    【解决方案1】:

    使用object destructuring获取属性,使用shorthand property names生成新对象:

    const dummyArray = [{ "att20": "att20", "att30": "att30", "att70": "att70", "att80": "att80"}, { "att20": "att20", "att30": "att30", "att70": "att70", "att80": "att80"}];
    
    const result = dummyArray.map(({ att20, att30, att70, att80 }) => ({
      att20, 
      att30, 
      att70, 
      att80
    }));
    
    console.log(result);
    【解决方案2】:

    将要保留的道具存储在数组中,然后为每个对象将所需道具转移到新对象。

    var dummyArray = [{ "att1": "something", "att2": "something", "att100": "something"}, { "att1": "something", "att2": "something", "att100": "something"}];
    
    var propsToKeep = ["att1", "att100"];
    
    var result = dummyArray.map(item => {
      const obj = {};
      for (const prop of propsToKeep) {
        obj[prop] = item[prop];
      }
      return obj;
    })
    
    console.log(result)

    【讨论】:

    • 这对于通用功能来说很好,但是当键列表是先验已知时效率低下。
    • @Alnitak 是的,但即在 ori-dori 和 brk 解决方案中存在代码重复(键名重复)。我认为代码重复(以及任何过早的优化混乱)比更慢但更灵活的代码更糟糕
    【解决方案3】:

    map 创建一个新数组,所以不需要删除任何东西,而是创建一个 有趣的键数组并返回它

    var dummyArray = [{
      "att1": "something",
      "att2": "something",
      "att20": "something",
      "att100": "something"
    }, {
      "att1": "something",
      "att2": "something",
      "att20": "something",
    
      "att100": "something"
    }];
    
    let x = dummyArray.map((item) => {
      return {
        attr20: item.att20
      }
    
    })
    
    console.log(x)

    【讨论】:

      【解决方案4】:

      这里有一个函数,它接受一个对象,只提取你想要的属性。你通过一个数组作为第二个参数。

      优点:更直接、更干净。特别是当您只需要从一个对象中提取时。

      如果你有一个对象列表。遍历列表并在每次迭代中提取。

      function objectExtract(obj, properties) {
          return properties.reduce((result, prop) => {
              if (obj.hasOwnProperty(prop)) {
                  result[prop] = obj[prop];
              }
              return result;
          }, {});
      }
      

      在此处了解 reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

      使用:

      (redux 的一个真实例子)

      store.dispatch(
                  setRooms({ 
                      ...roomsState,
                      list: rooms.reduce((list, room) => {
                          list[room.id] = objectExtract(room, ['name', 'description', 'createdAt', 'updatedAt']);
                          return list;
                      }, {})
                  })
              )
      

      (关于问题的例子)

      var dataSourceArray = [{
        "att1": "something",
        "att2": "something",
        "att20": "something",
        "att100": "something"
      }, {
        "att1": "something",
        "att2": "something",
        "att20": "something",
      
        "att100": "something"
      }];
      
      let x = dataSourceArray.map((item) => {
        return objectExtrac(item, ['att100', 'att2']);
      });
      

      【讨论】:

      • 其他答案中的传播运算符... 给我带来了麻烦。正版功能objectExtract 非常适合我。
      【解决方案5】:

      我找到了最简单的方法 JSON.stringify()

      JSON.stringify() 方法将 JavaScript 对象或值转换为 JSON 字符串,如果指定了替换函数,则可选地替换值,或者如果指定了替换器数组,则可选地仅包括指定的属性。

          const odata = [
            { "id": "0001", "type": "donut", "name": "Cake", "ppu": 0.55 },
            { "id": "0002", "type": "ansd", "name": "EARK", "ppu": 0.67 }
          ];
          const outdata=JSON.stringify(odata,['id','type']);
          console.log(outdata);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-17
        • 2019-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-08
        • 2019-06-22
        • 2010-11-10
        相关资源
        最近更新 更多