克隆javascript对象,忽略一个属性

这个问题已经在这里有了答案:            >            Simplest way to copy JS object and filter out certain properties                                    6个
>            Remove key-value pair from JSON object                                    6个
>            Javascript – Removing object key not using delete                                    2个
JavaScript中返回仅忽略一个或多个属性的对象的最佳方法是什么?

我可以将一个键分配给undefined并且可以肯定地工作,但是如果要完全摆脱那个键怎么办?

function removeCKey() {
  const obj = {a: 'a',b: 'b',c: 'c'}
  return {
    ...obj,c: undefined,};
}

const myObj = removeCKey();

另外,我想避免在这样使用散布运算符的地方创建中间对象

function removeCKey() {
  const obj = {a: 'a',c: 'c'}
  const {c,...rest} = newObj

  return rest;
}

const myObj = removeCKey();
最佳答案
您可以使用ES6 object destructuring assignment.

function removeKeys() {
  const obj = {
    a: 'a',c: 'c'
  };

  // extract property c in variable c
  // and rest of the value into res 
  let { c,...res } = obj;

  return res;
}

console.log(removeKeys())

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...