How to Remove a Key from a JavaScript Object

How do I remove a key from a JavaScript object?

The delete operator allows you to remove a property from an object.

The following examples all do the same thing.

// Example 1
var key = "Cow";
delete thisIsObject[key];

// Example 2
delete thisIsObject["Cow"];

// Example 3
delete thisIsObject.Cow;

let animals = {
'Cow': 'Moo',
'Cat': 'Meow',
'Dog': 'Bark'
};

delete animals.Cow;
delete animals['Dog'];

console.log(animals);

How do I remove a property from a JavaScript object?

To remove a property from an object (mutating the object), you can do it like this:

delete myObject.regex;
// or,
delete myObject['regex'];
// or,
var prop = "regex";
delete myObject[prop];

Demo

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

console.log(myObject);

Delete a key under a condition in Javascript object

You could use the regex /^[aeiouy]/. The ^ in the regex matches the start of the string [aeiouy] matches one of a, e, i, o, u, or y. To match case in-sensitive (ignore character casing) add the i flag. eg. /^[aeiouy]/i

const vowelsObj = {
alarm: 'hello',
chip: 100,
isValid: false,
Advice: 'Learn it hard',
onClick: 'make it great again',
}

function removeVowelKeys(object) {
for (let key in object) {
if (key.match(/^[aeiouy]/)) {
delete object[key];
}
}
return object;
}

removeVowelKeys(vowelsObj);
console.log(vowelsObj);

How to remove a key from inner object through lodash

You have to loop over the array and use omit

Arguments of omit is

1) object: The source object.

2) [paths] (…(string|string[])): The property paths to omit.

Return value

(Object): Returns the new object.

const clone = result.map((obj) => ({
...obj,
users: obj.users.map((o) => _.omit(o, ["color"])),
}));

Live Demo

Codesandbox Demo

You can easily achieve the result using vanilla JS using map as:

var result = [
{
color: "blue",
users: [
{
name: "John",
color: "blue",
age: "29",
},
{
name: "Neil",
color: "blue",
age: "34",
},
],
},
{
color: "green",
users: [
{
name: "Ronn",
color: "green",
age: "50",
},
],
},
];

const res = result.map((obj) => ({ ...obj, users: obj.users.map(({ color, ...rest }) => rest)}));
console.log(res);

In Javascript remove keys from object not in an Array

A couple of improvements.

  1. You're using .map() which creates a new array, but then you're just assigning it to the old variable. So, you apparently don't need to create a new array at all, you can just iterate the one you have and modify it.

  2. Put the properties you want to keep in a Set instead of an Array for potentially faster lookup.

  3. for/of loops are generally favored over .forEach() loops because of better flow control options (break, continue, return, etc...) and more opportunities for compiler optimization.

let kv = [{
'a': 1,
'b': 2,
'c': 3
},
{
'a': 1,
'b': 2,
'c': 3,
'd': 4
}]

const l = new Set(['a', 'b']);

for (let obj of kv) {
for (let prop of Object.keys(obj)) {
if (!l.has(prop)) {
delete obj[prop];
}
}
}

console.log(kv);

How can I remove the parent keys from a javascript Object?

Given Object:

schoolsObject = [{
"college_1":{
"id":"college_1",
"location":"Victoria",
"name":"College One"
},
"college_2":{
"id":"college_2",
"location":"Tasmania",
"name":"College Two"
}
}];

Solution:

Object.values(schoolsObject[0]);

Result:

[{
"id":"college_1",
"location":"Victoria",
"name":"College One"
},{
"id":"college_2",
"location":"Tasmania",
"name":"College Two"
}]

Remove object Key of an javascript object

You need to store the result in an array as follows:

const data = {
"xx2b": { "county": "Baringo", "town": "Kabarnet", "saccoRegistration": "BO2/08/009", "saccoName": "Baringo2" },
"QQDa": { "saccoRegistration": "Ba/09/009", "town": "Mogotio", "county": "Baringo", "saccoName": "Baringo1" }
};

const res = Object.entries(data).map(([id, props]) => ({ ...props, id }));

console.log(res);


Related Topics



Leave a reply



Submit