考虑到一个对象:
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
如何删除财产 regex 以完成下一个 myObject?
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI"
};
考虑到一个对象:
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
如何删除财产 regex 以完成下一个 myObject?
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI"
};
当前回答
我用Lodash的“Unset”来让它发生在一个被遗弃的对象中......这只需要写小逻辑来找到由遗弃方法所期望的财产密钥的路径。
返回财产路径作为序列的方法
var a = {"bool":{"must":[{"range":{"price_index.final_price":{"gt":"450", "lt":"500"}}}, {"bool":{"should":[{"term":{"color_value.keyword":"Black"}}]}}]}}; function getPathOfKey(object,key,currentPath, t){ var currentPath = currentPath || []; for(var i in object){ if(i == key){ t = currentPath; } else if(typeof object[i] == "object"){ currentPath.push(i) return getPathOfKey(object[i], key,currentPath) } } t.push(key); return t; } document.getElementById("output").innerHTML =JSON.stringify(getPathOfKey(a,"price_index.final_price")) <div id="output"> </div>
然后只使用Lodash unset方法从对象中移除财产。
var unset = require('lodash.unset'); unset(a, getPathOfKey(a,“price_index.final_price”));
其他回答
我用Lodash的“Unset”来让它发生在一个被遗弃的对象中......这只需要写小逻辑来找到由遗弃方法所期望的财产密钥的路径。
返回财产路径作为序列的方法
var a = {"bool":{"must":[{"range":{"price_index.final_price":{"gt":"450", "lt":"500"}}}, {"bool":{"should":[{"term":{"color_value.keyword":"Black"}}]}}]}}; function getPathOfKey(object,key,currentPath, t){ var currentPath = currentPath || []; for(var i in object){ if(i == key){ t = currentPath; } else if(typeof object[i] == "object"){ currentPath.push(i) return getPathOfKey(object[i], key,currentPath) } } t.push(key); return t; } document.getElementById("output").innerHTML =JSON.stringify(getPathOfKey(a,"price_index.final_price")) <div id="output"> </div>
然后只使用Lodash unset方法从对象中移除财产。
var unset = require('lodash.unset'); unset(a, getPathOfKey(a,“price_index.final_price”));
我们可以使用
使用删除 object.property 使用删除 object[‘property’] 使用休息,删除多个属性
让 myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*”, “regex1”: “^http://.*”, “regex2”: “^http://.*”, “regex3”: “^http://.*”, “regex4”: “^http://.*” }; 删除 myObject.regex; // 使用删除 object.property // 或删除 myObject(‘regex1’); // 使用删除 object(‘property’) const { regex2, regex3, regex4,...newMyObject } = myObject; console.log(n)
这个帖子很古老,我觉得它非常有用,所以我决定分享我写的 unset 函数,如果其他人看到这个帖子,想想为什么它不那么简单,因为它在 PHP unset 函数。
写下这个新的 unset 函数的原因,是保持所有其他变量的指数在这个 hash_map. 查看下面的例子,看看如何在从 hash_map 中删除值后没有改变“test2”的指数。
function unset(unsetKey, unsetArr, resort) { var tempArr = unsetArr; var unsetArr = {}; delete tempArr[unsetKey]; if (resort) { j = -1; } for (i in tempArr) { if (typeof(tempArr[i]) !== 'undefined') { if (resort) { j++; } else { j = i; } unsetArr[j] = tempArr[i]; } } return unsetArr; } var unsetArr = ['test', 'deletedString', 'test2']; console.log(unset('1', unsetArr, true)); // output Object {0: "test", 1: "test2"} console.log(unset('1', unsetArr, false)); // output Object {0: "test", 2: "test2"}
var myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; 删除 myObject.regex; console.log ( myObject.regex); // logs: undefined
它在Firefox和Internet Explorer工作,我认为它在其他所有工作。
如果你不想修改原始对象。
移除物品,而无需转换物品
如果移动性是一种担忧,你可以通过从旧的所有属性复制创建一个全新的对象,除了你想要删除的所有属性。
让 myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” };让 prop = “regex”; const updatedObject = Object.keys(myObject).reduce((object, key) => { if (key!== prop) { object[key] = myObject[key] } return object }, {}) console.log(updatedObject);