根据对象的位置删除对象的 属性
Delete an object's property given its location
给定数组形式的对象的属性的位置([a, b, c]
对应object.a.b.c
),我如何执行语句的等价物delete object.a.b.c
?
编辑:澄清一下,我想编写一个如下所示的函数:
function deleteProperty(object, location) {
// do stuff
}
并具有以下效果:
var obj = {"foo": "FOO", "bar": "BAR", "a": {"b": "B"}};
deleteProperty(obj, [a, b]);
typeof obj.a.b === "undefined";
如果您喜欢使用图书馆,请尝试 lodash's _.unset
。此示例直接来自文档:
Removes the property at path of object.
Note: This method mutates object.
var object = { 'a': [{ 'b': { 'c': 7 } }] };
_.unset(object, 'a[0].b.c');
// => true
console.log(object);
// => { 'a': [{ 'b': {} }] };
_.unset(object, ['a', '0', 'b', 'c']);
// => true
console.log(object);
// => { 'a': [{ 'b': {} }] };
在你的情况下,你会这样做:
var obj = { foo: 'FOO', bar: 'BAR', a: { b: 'B' } };
_.unset(obj, ['a', 'b']);
typeof obj.a.b === 'undefined'; // => true
如果你想用自己的代码来模仿它,你可以看看他们是如何实现的on line 4155 of the source:
/**
* The base implementation of `_.unset`.
*
* @private
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to unset.
* @returns {boolean} Returns `true` if the property is deleted, else `false`.
*/
function baseUnset(object, path) {
path = isKey(path, object) ? [path] : castPath(path);
object = parent(object, path);
var key = toKey(last(path));
return !(object != null && hasOwnProperty.call(object, key)) || delete object[key];
}
不过,您必须查找其中使用的每个函数。
在此声明中
deleteProperty(obj, [a, b]);
a
和 b
是变量名,这是不正确的语法。
如果传递字符串名称,则可以实现此功能:
deleteProperty(obj, ["a", "b"]);
它可能看起来像这样:
function deleteProperty(obj, location)
{
var finalPropertyName = location.pop();
location.forEach(function(key) { obj = obj[key]; });
delete obj[finalPropertyName];
}
var obj = { foo: 'FOO', bar: 'BAR', a: { b: 'B' } };
deleteProperty(obj, ["a", "b"]);
console.log(obj);
var obj2 = { foo: 'FOO', bar: 'BAR', a: { b: 'B' } };
deleteProperty(obj2, ["a"]);
console.log(obj2);
当然,这个片段只是一个想法。您可以更好地实施它,或添加任何功能,例如检查现有属性。
您需要使用括号表示法并循环直到到达最后一个,然后使用最后一个进行删除。
function deleteProperty(obj, loc) {
var last = loc.pop(); //grab last item from array
var x = loc.reduce(function(o, x) { //walk obj until all properties are there
return o[x];
}, obj);
delete x[last]; //delete the last item from the array
}
var obj = {
"foo": "FOO",
"bar": "BAR",
"a": {
"b": "B"
}
};
deleteProperty(obj, ["a", "b"]);
console.log(obj.a.b);
代码假定路径将在那里,没有检查 null/undefined/invalid 路径。
给定数组形式的对象的属性的位置([a, b, c]
对应object.a.b.c
),我如何执行语句的等价物delete object.a.b.c
?
编辑:澄清一下,我想编写一个如下所示的函数:
function deleteProperty(object, location) {
// do stuff
}
并具有以下效果:
var obj = {"foo": "FOO", "bar": "BAR", "a": {"b": "B"}};
deleteProperty(obj, [a, b]);
typeof obj.a.b === "undefined";
如果您喜欢使用图书馆,请尝试 lodash's _.unset
。此示例直接来自文档:
Removes the property at path of object.
Note: This method mutates object.
var object = { 'a': [{ 'b': { 'c': 7 } }] };
_.unset(object, 'a[0].b.c');
// => true
console.log(object);
// => { 'a': [{ 'b': {} }] };
_.unset(object, ['a', '0', 'b', 'c']);
// => true
console.log(object);
// => { 'a': [{ 'b': {} }] };
在你的情况下,你会这样做:
var obj = { foo: 'FOO', bar: 'BAR', a: { b: 'B' } };
_.unset(obj, ['a', 'b']);
typeof obj.a.b === 'undefined'; // => true
如果你想用自己的代码来模仿它,你可以看看他们是如何实现的on line 4155 of the source:
/**
* The base implementation of `_.unset`.
*
* @private
* @param {Object} object The object to modify.
* @param {Array|string} path The path of the property to unset.
* @returns {boolean} Returns `true` if the property is deleted, else `false`.
*/
function baseUnset(object, path) {
path = isKey(path, object) ? [path] : castPath(path);
object = parent(object, path);
var key = toKey(last(path));
return !(object != null && hasOwnProperty.call(object, key)) || delete object[key];
}
不过,您必须查找其中使用的每个函数。
在此声明中
deleteProperty(obj, [a, b]);
a
和 b
是变量名,这是不正确的语法。
如果传递字符串名称,则可以实现此功能:
deleteProperty(obj, ["a", "b"]);
它可能看起来像这样:
function deleteProperty(obj, location)
{
var finalPropertyName = location.pop();
location.forEach(function(key) { obj = obj[key]; });
delete obj[finalPropertyName];
}
var obj = { foo: 'FOO', bar: 'BAR', a: { b: 'B' } };
deleteProperty(obj, ["a", "b"]);
console.log(obj);
var obj2 = { foo: 'FOO', bar: 'BAR', a: { b: 'B' } };
deleteProperty(obj2, ["a"]);
console.log(obj2);
当然,这个片段只是一个想法。您可以更好地实施它,或添加任何功能,例如检查现有属性。
您需要使用括号表示法并循环直到到达最后一个,然后使用最后一个进行删除。
function deleteProperty(obj, loc) {
var last = loc.pop(); //grab last item from array
var x = loc.reduce(function(o, x) { //walk obj until all properties are there
return o[x];
}, obj);
delete x[last]; //delete the last item from the array
}
var obj = {
"foo": "FOO",
"bar": "BAR",
"a": {
"b": "B"
}
};
deleteProperty(obj, ["a", "b"]);
console.log(obj.a.b);
代码假定路径将在那里,没有检查 null/undefined/invalid 路径。