javascript从数组中的所有对象中提取某些属性

时间:2018-09-03 16:17:09

标签: javascript arrays ecmascript-6

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

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并删除其余的对象?

5 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

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)

答案 2 :(得分:1)

我找到了最简单的方法 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);

答案 3 :(得分:0)

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

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)

答案 4 :(得分:0)

这里有一个接受对象的函数,并且仅提取所需的属性。您将其作为第二个参数传递给数组。

优点:更直接,更清洁。特别是当您只需要从一个对象中提取内容时。

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

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

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']);
});