考虑到一个对象:

let myObject = {
  "ircEvent": "PRIVMSG",
  "method": "newURI",
  "regex": "^http://.*"
};

如何删除财产 regex 以完成下一个 myObject?

let myObject = {
  "ircEvent": "PRIVMSG",
  "method": "newURI"
};

当前回答

Object.assign() 和 Object.keys() 和 Array.map()

const obj = { “过滤器”:[ { “过滤器类型”:”之间”, “区域”:”BasicInformationRow.A0”, “MaxValue”:”2017-10-01”, “MinValue”:”2017-09-01”, “值”:”过滤器值” } } }; let new_obj1 = Object.assign({}, obj.Filters[0]); let new_obj2 = Object.assign({}, obj.Filters[0]); /* // old version let sh

其他回答

在JavaScript中删除财产

突变物品属性删除,不安全

此类别适用于使用对象字母或对象例子时,您希望保持/继续使用原始参考,并且在您的代码中不使用无标志的功能原则。

'use strict'
const iLikeMutatingStuffDontI = { myNameIs: 'KIDDDDD!', [Symbol.for('amICool')]: true }
delete iLikeMutatingStuffDontI[Symbol.for('amICool')] // true
Object.defineProperty({ myNameIs: 'KIDDDDD!', 'amICool', { value: true, configurable: false })
delete iLikeMutatingStuffDontI['amICool'] // throws

基于回归的链条财产遗弃

此类别适用于在更新的 ECMAScript 品味中运行平面对象或序列示例,当不变化的方法是需要的,并且您不需要为符号密钥负责:

const foo = { name: 'KIDDDDD!', [Symbol.for('isCool')]: true }
const { name, ...coolio } = foo // coolio doesn't have "name"
const { isCool, ...coolio2 } = foo // coolio2 has everything from `foo` because `isCool` doesn't account for Symbols :(

突变物品属性删除,安全

'use strict'
const iLikeMutatingStuffDontI = { myNameIs: 'KIDDDDD!', [Symbol.for('amICool')]: true }
Reflect.deleteProperty(iLikeMutatingStuffDontI, Symbol.for('amICool')) // true
Object.defineProperty({ myNameIs: 'KIDDDDD!', 'amICool', { value: true, configurable: false })
Reflect.deleteProperty(iLikeMutatingStuffDontI, 'amICool') // false

const foo = { name: 'KIDDDDD!', [Symbol.for('isCool')]: true }
const { name, ...coolio } = foo // coolio doesn't have "name"
const { isCool, ...coolio2 } = foo // coolio2 has everything from `foo` because `isCool` doesn't account for Symbols :(

基于图书馆的财产遗弃

这个类别通常允许更大的功能灵活性,包括对符号的会计 & 在一个声明中忽略多个属性:

const o = require("lodash.omit")
const foo = { [Symbol.for('a')]: 'abc', b: 'b', c: 'c' }
const bar = o(foo, 'a') // "'a' undefined"
const baz = o(foo, [ Symbol.for('a'), 'b' ]) // Symbol supported, more than one prop at a time, "Symbol.for('a') undefined"

使用Lodash

import omit from 'lodash/omit';

const prevObject = {test: false, test2: true};
// Removes test2 key from previous object
const nextObject = omit(prevObject, 'test2');

使用RAMDA

R.omit(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, c: 3}

这个帖子很古老,我觉得它非常有用,所以我决定分享我写的 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 Hogwarts = {
    staff : [
        'Argus Filch',
        'Filius Flitwick',
        'Gilderoy Lockhart',
        'Minerva McGonagall',
        'Poppy Pomfrey',
        ...
    ],
    students : [
        'Hannah Abbott',
        'Katie Bell',
        'Susan Bones',
        'Terry Boot',
        'Lavender Brown',
        ...
    ]
};

删除物品财产

delete Hogwarts.staff;

delete Hogwarts['staff'];

删除 array index

现在,如果你想移除一个员工或学生,程序有点不同,因为两种属性都是由自己组成的。

Hogwarts.staff.splice(3, 1);

Hogwarts.staff.splice(Hogwarts.staff.indexOf('Minerva McGonnagall') - 1, 1);

笔记

虽然你技术上可以使用删除一个序列,使用它会导致收到错误的结果,当打电话,例如,Hogwarts.staff.length 后来。

因此,在从对象中删除值时,首先要考虑您是否正在处理对象属性,或者您是否正在处理序列值,并根据此选择适当的策略。

如果你想尝试这个,你可以使用这个Fiddle作为一个起点。

我用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”));