javascript从数组中的所有对象中提取某些属性 [英] javascript extract certain properties from all objects in array

查看:134
本文介绍了javascript从数组中的所有对象中提取某些属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an array of objects with the same properties. Each object has around a hundred properties. I want to keep only a handful of them in a new array:

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

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

How can I filter/map/reduce... and extract the interesting keys?

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

我如何仅保留每个对象的att20att30att70att80并删除其余对象?

how can I keep only att20, att30, att70, att80 for each object and delete the rest?

推荐答案

使用

Use object destructuring to get the properties, and generate a new object using 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);

这篇关于javascript从数组中的所有对象中提取某些属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆