我有一个平面JS对象:
{a: 1, b: 2, c: 3, ..., z:26}
我想克隆除一个元素之外的对象:
{a: 1, c: 3, ..., z:26}
执行此操作的最简单方法是什么(如果可能,最好使用ES6/7)?
我有一个平面JS对象:
{a: 1, b: 2, c: 3, ..., z:26}
我想克隆除一个元素之外的对象:
{a: 1, c: 3, ..., z:26}
执行此操作的最简单方法是什么(如果可能,最好使用ES6/7)?
如果使用Babel,则可以使用以下语法转换为copy property b from x into variable b and then copy rest of properties into variable y:
let x = {a: 1, b: 2, c: 3, z:26};
let {b, ...y} = x;
"use strict";
function _objectWithoutProperties(obj, keys) {
var target = {};
for (var i in obj) {
if (keys.indexOf(i) >= 0) continue;
if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
target[i] = obj[i];
}
return target;
}
var x = { a: 1, b: 2, c: 3, z: 26 };
var b = x.b;
var y = _objectWithoutProperties(x, ["b"]);