考虑到一个对象:

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

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

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

当前回答

您可以使用 Delete Property 删除对象中的财产[键]

其他回答

尝试下面的方法. 将对象属性值分为未定义。

var myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; myObject.regex = undefined; myObject = JSON.parse(JSON.stringify(myObject)); console.log(myObject);

一个没有财产的物体的克隆:

例如:

let object = { a: 1, b: 2, c: 3 };

我们必须删除A。

有一个明确的 prop 密钥: const { a,...rest } = 对象; 对象 = 休息; 有一个变量的 prop 密钥: const propKey = 'a'; const { [propKey]: propValue,...rest } = 对象; 对象 = 休息; A cool arrow 函数 <unk>: const removeProperty = (propKey, { [propKey]: propValue,...rest }) => 休息; 对象 = removeProperty('a', 对象); 多种属性 const removeProperties = (object,...keys)

使用

object = removeProperties(object, 'a', 'b') // result => { c: 3 }

const propsToRemove = ['a', 'b']
object = removeProperties(object, ...propsToRemove) // result => { c: 3 }

讓 myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” }; obj = Object.fromEntries( Object.entries(myObject).filter(功能(m){ return m[0]!= “regex”/*或任何關鍵去刪除*/ } ) console.log(obj)

您也可以使用 Object.entries 处理对象类似于 a2d 序列,并使用 序列删除对象,如您在正常序列中一样,或者简单地通过对象过滤,如您在序列中一样,并将重建的对象归还原始变量。

这个帖子很古老,我觉得它非常有用,所以我决定分享我写的 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"}

const obj = { foo: "bar" };

delete obj.foo;
obj.hasOwnProperty("foo"); // false

arr;             // [0, 1, 2, 3, 4]
arr.splice(3,1); // 3
arr;             // [0, 1, 2, 4]

let parent = {
    member: { str: "Hello" }
};
let secondref = parent.member;

delete parent.member;
parent.member;        // undefined
secondref;            // { str: "Hello" }

另一个重要缺点是,删除操作员不会为您重新组织结构,这有可能看起来反直觉的结果。

let array = [0, 1, 2, 3]; // [0, 1, 2, 3]
delete array[2];          // [0, 1, empty, 3]

let fauxarray = {0: 1, 1: 2, length: 2};
fauxarray.__proto__ = [].__proto__;
fauxarray.push(3);
fauxarray;                // [1, 2, 3]
Array.isArray(fauxarray); // false
Array.isArray([1, 2, 3]); // true

任何方法使用 Symbol.iterator 将返回不确定的值在指数. forEach,地图和减少将简单地错过错过的指数,但不会删除它

例子:

let array = [1, 2, 3]; // [1,2,3]
delete array[1];       // [1, empty, 3]
array.map(x => 0);     // [0, empty, 0]

Array#splice 改变序列,并返回任何被删除的指标. deleteCount 元素从指标开始移除,并且 item1, item2... itemN 从指标开始插入序列. 如果 deleteCount 被忽略,则从 startIndex 元素被移除到序列结束。

let a = [0,1,2,3,4]
a.splice(2,2) // returns the removed elements [2,3]
// ...and `a` is now [0,1,4]

首頁 〉外文書 〉文學 〉西洋文學 〉Array#slice([start[, end])

let a = [0,1,2,3,4]
let slices = [
    a.slice(0,2),
    a.slice(2,2),
    a.slice(2,3),
    a.slice(2,5) ]

//   a           [0,1,2,3,4]
//   slices[0]   [0 1]- - -   
//   slices[1]    - - - - -
//   slices[2]    - -[3]- -
//   slices[3]    - -[2 4 5]

首頁 #pop

交换#Shift