从对象中删除元素 - javascript [英] removing elements from objects - javascript
本文介绍了从对象中删除元素 - javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个对象数组,例如
if I had an array of objects such as
const arrOfObj = [
{
'a': 'a',
'b': 'b',
'c': 'c',
'd': 'd'
}
]
如何使用只有a和b的对象创建一个新数组?
how do I create a new array with an object that has only a and b?
{
'a': 'a',
'b': 'b'
}
我一直在尝试像
Object.entries(arrOfObjs[0]).filter(x => x[0] !== 'a' && x[0] !== 'b')
但必须有一个更简单的清洁方式
but there must be a simpler cleaner way
推荐答案
你可以 map
数组,使用 destructuring 和简写属性名称你在那里:
You can map
the array, use destructuring and shorthand property names you're there:
const result = arrOfObj.map(({a, b}) => ({a, b}));
示例:
const arrOfObj = [
{ 'a': 'a', 'b': 'b', 'c': 'c', 'd': 'd' },
{ 'a': 'aa', 'b': 'bb', 'c': 'cc', 'd': 'dd' },
{ 'a': 'aaa', 'b': 'bbb', 'c': 'ccc', 'd': 'ddd' }
];
const result = arrOfObj.map(({a, b}) => ({a, b}));
console.log(result);
这篇关于从对象中删除元素 - javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文