简体   繁体   English

从对象中删除元素-javascript

[英]removing elements from objects - javascript

if I had an array of objects such as 如果我有一系列对象,例如

const arrOfObj = [
    {
        'a': 'a', 
        'b': 'b', 
        'c': 'c',
        'd': 'd'
    }
 ]

how do I create a new array with an object that has only a and b? 如何使用只有a和b的对象创建新数组?

 {
        'a': 'a', 
        'b': 'b'
 }

I've been trying something like 我一直在尝试类似

Object.entries(arrOfObjs[0]).filter(x => x[0] !== 'a' && x[0] !== 'b')

but there must be a simpler cleaner way 但必须有一种更简单的清洁方法

You can map the array, use destructuring and shorthand property names you're there: 您可以map数组,使用解构简写属性名称

const result = arrOfObj.map(({a, b}) => ({a, b}));

Example: 例:

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

Destructuring syntax works nicely for this, as you can see in the other answer. 如您在其他答案中所见,销毁语法对此非常有效。 However it requires a and b at the program level. 但是,它在程序级别需要ab If a and b exist at the data level, for example as values in array, the program would be slightly different 如果ab在数据级别存在(例如,作为数组中的值),则程序将略有不同

 const pick = (o = {}, keys = []) => keys .reduce ( (acc, k) => Object .assign (acc, { [k]: o[k] }) , {} ) const print = (...values) => values .forEach (x => console .log (x)) const data = { a: 1, b: 2, c: 3 } print ( pick (data, [ 'a', 'b' ]) // { a: 1, b: 2 } , pick (data, [ 'c', 'd' ]) // { c: 3, d: undefined } , pick (data, []) // {} , data // { a: 1, b: 2, c: 3 } ) 

As you can see, data is not mutated by use of pick . 如您所见, data不会通过使用pick突变。

Your title suggests that you want to remove specific properties, but the text suggests you actually want to keep specific ones. 标题表明您想要删除特定的属性,但是文本表明您实际上想要保留特定的属性。 If it's the latter, I would suggest either the solution from ibrahim mahrir or the one from user633183. 如果是后者,我建议是易卜拉欣·马尔里尔(ibrahim mahrir)的解决方案,或者是user633183的解决方案。

But if it's the former, here is an approach similar to the one from user633183 but that creates a clone without the given properties: 但是,如果是前者,则这是一种类似于user633183的方法,但是会创建一个具有给定属性的克隆:

 const omit = (keys = []) => (o = {}) => Object .keys (o) .reduce ( (acc, k) => keys .includes (k) ? acc : Object .assign (acc, { [k]: o[k] }) , {} ) const print = (...values) => values .forEach (x => console .log (x)) const data = { a: 1, b: 2, c: 3, d: 4 } print ( omit ([ 'c', 'd' ]) (data) // { a: 1, b: 2 } , omit ([ 'b' ]) (data) // { a: 1, c: 3, d: 4 } , omit ([ ]) (data) // { a: 1, b: 2, c: 3, d: 4 } , data // { a: 1, b: 2, c: 3, d: 4 } ) 

You can delete properties from an object. 您可以从对象中删除属性。

 const arrOfObj = [ { 'a': 'a', 'b': 'b', 'c': 'c', 'd': 'd' } ]; console.log(arrOfObj.reduce((acc, val) => { let v = Object.assign({}, val); Object.keys(v).forEach(i => { if (i !== 'a' && i !== 'b') { delete v[i]; } }); acc.push(v); return acc; }, [])); console.log(arrOfObj); 

** Edited the above to stay more inline with the request. **编辑以上内容以使其与请求保持更加内联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM