简体   繁体   English

如何使用对象解构扩展运算符来删除多个对象属性

[英]How to use object destructuring spread operator to delete multiple object properties

I am using in my reducers Map/Associative array to store object via string ID. 我在我的Redurs Map / Associative数组中使用通过字符串ID存储对象。

When I need new item into Map I am using this construction 当我需要新项目到Map时我正在使用这种结构

rows: { ...state.rows, [row.id]: row }

When I need delete item form map by Id I am using this. 当我需要通过Id删除项目表单映射时我正在使用它。

const  { [rowId]: deleted, ...rows } = state.rows;

Than in variable rows I have map and property with name rowId is missing. 在变量行中,我有地图和名称为rowId属性丢失。

I am wondering how can i do this if i have multiple Ids in array and I need delete all of them via ... operator and destructuring. 我想知道如果我在数组中有多个Ids我怎么能这样做我需要通过...运算符和解构删除所有这些。

const contentIds = ['id1','id2','id3']

// something like ???
const {...[ids], ...rows} 

Yes I can write lamda for that or use omit lodash. 是的我可以为此写lamda或使用省略lodash。 But just interesting if it is possible. 但如果可能的话,这很有趣。

Thanks very much 非常感谢

You could reduce the keys with the object. 您可以使用该对象减少键。

 var object = { id: 1, id1: 2, id2: 3 }, contentIds = ['id1', 'id2', 'id3'], result = contentIds.reduce((o, k) => ({ [k]: _, ...r } = o, r), object); console.log(result); 

const myObj = { id1: 'test', id2: 'test', id3: 'test' }
const idsToFilter = ['id1', 'id2']
const { id1, id2, ...filtered } = myObj
console.log(filtered)

However you can't do that programmatically unless you know the exact ids you want to filter from the object however. 但是,除非您知道要从对象中过滤的确切ID,否则无法以编程方式执行此操作。 if you want to do it based on an array of ids instead, you'd need to use something like this 如果你想基于一组id来做它,你需要使用这样的东西

function filterObjectByKeys (object, keysToFilter) {
  return Object.keys(object).reduce((accum, key) => {
    if (!keysToFilter.includes(key)) {
      return { ...accum, [key]: object[key] }
    } else {
      return accum
    }
  }, {})
}

I am wander how can i do this if i have multiple Ids in array and I need delete all of them via ... operator and destructuring. 我很徘徊,如果我在阵列中有多个ID,我怎么能这样做,我需要通过...运算符和解构删除所有这些。

No you can't do this, you're probably getting confused with rest and spread syntax, ... on the left hand side of assignment is rest syntax not spread 不,你不能这样做,你可能会对restspread语法感到困惑, ...在任务的左侧是rest语法没有spread

Rest and Spread 休息和传播

And rest syntax expects ... followed by variable name, what you're trying is syntaxError rest语法需要...后跟变量名,你正在尝试的是syntaxError

 const object = {a:1,b:2,c:3} const a = ['a','b'] const {...[a], ...rows} = object console.log(rows) 

More over you can't use , tralling comma after rest so even if you do 更多的你不能使用, rest后逗留逗号,即使你这样做

let {...a, ...rows } = obj 

it's still invalid 它仍然无效

 const object = {a:1,b:2,c:3} const {...a, ...rows} = object console.log(rows) 

You can use the solution suggested by @nina or you can create a copy of object and loop through contentIds and delete key/value from copyObj to get desired value 您可以使用@nina建议的解决方案,或者您可以创建对象的副本并循环遍历contentIds并从copyObj中删除键/值以获得所需的值

 const object = { id: 1, id1: 2, id2: 3 }; const contentIds = ['id1', 'id2', 'id3']; const objCopy = object contentIds.forEach(key=>{ delete objCopy[key] }) console.log(objCopy); 

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

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