json - Retirer les propriétés des objets (JavaScript)

Mots clés : javascriptjsonregexobjectpropertiesjavascript

meilleur 5 Réponses json - Retirer les propriétés des objets (JavaScript)

vote vote

94

delete myObject.regex; // or, delete myObject['regex']; // or, var prop = "regex"; delete myObject[prop]; 
var myObject = {     "ircEvent": "PRIVMSG",     "method": "newURI",     "regex": "^http://.*" }; delete myObject.regex;  console.log(myObject);
let myObject = {   "ircEvent": "PRIVMSG",   "method": "newURI",   "regex": "^http://.*" };  const {regex, ...newObj} = myObject;  console.log(newObj);   // has no 'regex' key console.log(myObject); // remains unchanged
vote vote

80

var obj = {    myProperty: 1      }  console.log(obj.hasOwnProperty('myProperty')) // true  delete obj.myProperty  console.log(obj.hasOwnProperty('myProperty')) // false
vote vote

76

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

60

const { a, ...rest } = { a: 1, b: 2, c: 3 }; 
const myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; const { regex, ...newObject } = myObject; console.log(newObject); 
let myObject = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}; ({ regex, ...myObject } = myObject); console.log(myObject); 
vote vote

56

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] 
var array = [0, 1, 2, 3] delete array[2] // [0, 1, empty, 3] 
let a = [0,1,2,3,4] a.splice(2,2) // returns the removed elements [2,3] // ...and `a` is now [0,1,4] 
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] 

Questions similaires