考虑到一个对象:

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

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

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

当前回答

您可以使用如下的过滤器。

var myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” }; // Way 1 let filter1 = {} Object.keys({...myObject}).filter(d => { if(d!=='regex'){ filter1[d] = myObject[d]; }) console.log(filter1) // Way 2 let filter2 = Object.fromEntries(Object.entries({...myObject}).filter(d => d[0]!=='regex' )) console.log(filter2)

其他回答

短答

var obj = {
  data: 1,
  anotherData: 'sample'    
}
delete obj.data //this removes data from the obj

你被留在

var obj = {
  anotherData: 'sample'    
}

您可以使用如下的过滤器。

var myObject = { “ircEvent”: “PRIVMSG”, “method”: “newURI”, “regex”: “^http://.*” }; // Way 1 let filter1 = {} Object.keys({...myObject}).filter(d => { if(d!=='regex'){ filter1[d] = myObject[d]; }) console.log(filter1) // Way 2 let filter2 = Object.fromEntries(Object.entries({...myObject}).filter(d => d[0]!=='regex' )) console.log(filter2)

在JavaScript中,有2种常见方法可以从对象中删除属性。

第一個可轉換的方法是使用 delete object.property 運營商。

第二种方法是不可改变的,因为它不改变原始对象,是引用对象破坏和传播合成: const {财产,...rest} = 对象。

另一个解决方案,使用 Array#reduce。

(六)

const myObject = { ircEvent: 'PRIVMSG',方法: 'newURI', regex: '^http://.*', }; const myNewObject = Object.keys(myObject).reduce((obj, key) => { key!=='regex'? obj[key] = myObject[key] : null; return obj; }, {}); console.log(myNewObject);

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