简体   繁体   English

使用扩展运算符从 ES6 中的对象中删除目标参数

[英]Removing targeted parameter from Object in ES6 using spread operator

I am trying to remove a property from an object using the spread operator .我正在尝试使用扩展运算符从对象中删除属性。 Traditionally I have done this:传统上我是这样做的:

const original_object = { prop1 : 'string1', prop2: 'string2' };
const { prop1, ...rest } = original_object;

In the above situation, the removed property(prop1) will no longer exist within the rest object.在上述情况下,被移除的属性(prop1)将不再存在于其余对象中。

Suppose there is a more intricate property that I would like to remove, such as an object within the object.假设我想删除一个更复杂的属性,例如对象中的一个对象。

const original_object = {
    prop1: 'string1'
    prop2: {
        prop3: 'string3',
        prop4: 'string4'
    }
}
const { *remove prop3 of prop2 only here*, ...rest} = original_object;
console.log(prop3); // => 'string3';

What is the most elegant/easiest solution to do this?做到这一点最优雅/最简单的解决方案是什么? I want everything but the one prop3 of prop2 to be included in the object, in the exact same structure.我想要的一切,但一个prop3prop2被列入对象,在完全相同的结构。

Do it in two steps - first destructure the prop3 out, then create a new object, combining the rest of the outer object with the prop2 with the prop3 removed from it:分两步完成——首先将prop3解构出来,然后创建一个新对象,将外部对象的其余部分与 prop2 结合起来,并从中移除 prop3:

 const original_object = { prop1: 'string1', prop2: { prop3: 'string3', prop4: 'string4' } }; const { prop2: { prop3, ...restProp2 }, ...restOrig} = original_object; const newObj = { ...restOrig, prop2: restProp2 }; console.log(prop3); // => 'string3'; console.log(newObj);

While you could do this in just one statement with a hack, I wouldn't recommend it.虽然你可以在一个 hack 的语句中做到这一点,但我不推荐它。

If you find yourself doing this often with deeply nested properties, and don't like the extra line, maybe consider a helper function along the lines of:如果您发现自己经常使用深度嵌套的属性执行此操作,并且不喜欢额外的行,那么可以考虑使用以下行的辅助函数:

 const getNestedFrom = (obj, propStr) => { // make a copy, don't mutate the original object const newObj = JSON.parse(JSON.stringify(obj)); const props = propStr.split('.'); const lastProp = props.pop(); const lastObj = props.reduce((a, prop) => a[prop], newObj); const val = lastObj[lastProp]; delete lastObj[lastProp]; return [newObj, val]; }; const original_object = { prop1: 'string1', prop2: { prop3: 'string3', prop4: 'string4' } }; const [newObj, prop3] = getNestedFrom(original_object, 'prop2.prop3'); console.log(prop3); // => 'string3'; console.log(newObj);

Just for curiosity's sake (please don't do this), the hack would be to use a default property which definitely won't exist:出于好奇(请不要这样做),黑客将使用一个绝对不存在的默认属性:

 const original_object = { prop1: 'string1', prop2: { prop3: 'string3', prop4: 'string4' } }; const [ { prop2: { prop3, ...restProp2 }, ...restOrig}, newObj = { ...restOrig, prop2: restProp2 } ] = [original_object] console.log(prop3); // => 'string3'; console.log(newObj);

But that would be significantly (and needlessly) confusing.但这会显着(并且不必要地)令人困惑。

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

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