Is there a way to delete an object key and property? [duplicate] - javascript

This question already has answers here:
How do I remove a property from a JavaScript object?
(37 answers)
Closed 3 years ago.
I have an
let obj = { a:'Nasa', b:'Zezo'}
Is there a way to make that object look like obj = { a:'Nasa' }
I am not looking at delete operator because the key still exists.
the delete operator leaves me with {a:'Nasa', b:null }

let obj = { a:'Nasa', b:'Zezo'}
console.log(obj);
delete obj.b;
console.log(obj);
You can use delete obj.b for deleting b property

Related

Check object key has empty value [duplicate]

This question already has answers here:
How do I test for an empty JavaScript object?
(48 answers)
Closed 4 years ago.
I have object & its key empty value;
like var obj = {newData : {}};
I want to put if condition that return true or false. how can I check that obj.newData has empty object or not?
const obj = {newData : {}};
const obj2 = {newData : {x: 'x'}};
const isEmpty = testObj => Object.keys(testObj).length === 0;
console.log(isEmpty(obj.newData));
console.log(isEmpty(obj2.newData));

Adding a key from a variable string (es6) when using spread syntax [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 6 years ago.
I would like to know if there is a clean way to set the value of a key from a string variable when using spread syntax in es6?
Something like the following:
let keyVar = 'newKey'
let newObject = {keyVar:{some:'json'},...oldObject}
But this leads to:
{"keyVar":{"some":"json"}, ... }
rather than:
{"newKey":{"some":"json"}, ... }
You can use computed properties:
const keyVar = 'newKey';
const newObject = { [keyVar]: { some: 'json' } };
console.log(newObject);

jQuery expression as object key [duplicate]

This question already has answers here:
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I need to figure out how to create a dynamic key string for an object. This expression makes JavaScript complain.
return {$(this).val(): true}; // returns an object e.g. {2: true}
What am I doing wrong?
You have to create the object, then use bracket notation for the dynamic key
var obj = {};
var val = $(this).val();
obj[val] = true;
return obj;
or a completely unnecessary one-liner
return (function(o,e) {o[e.value]=true; return o;})({}, this);
The JavaScript object literal syntax {x: y} specifies that x will be a (possibly) quoteless string, and y any value. You can't use this syntax for dynamic keys.
Use this instead:
var foo = {};
foo[$(this).val()] = true;
return foo;

Javascript - retrieve object property path [duplicate]

This question already has answers here:
Convert string in dot notation to get the object reference [duplicate]
(6 answers)
Closed 8 years ago.
I have the following object
var obj = {};
obj.foo = {};
obj.foo.bar = "I want this";
given the "path" "foo.bar" as a string, how do I retrieve obj.foo.bar (or obj[foo][bar])?
Here's a way:
function getKey(key, obj) {
return key.split('.').reduce(function(a,b){
return a && a[b];
}, obj);
}
getKey('foo.bar', obj); //=> "I want this"
if path = "foo.bar" then you may write
var keys = path.split('.');
console.log(obj[keys[0]][keys[1]]);
just use the obj.foo.bar..that will work;

Javascript variable object keys [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 8 years ago.
I am attempting to add a variable key, with no luck.
Here's what I got so far:
mysql('translations',{
check: 'element_id',
element_id: element_id,
{'lang-'+lang_id}: value
});
The variable key is the last line of the function.
Any ideas?
You can't use expressions for the identifiers in an object literal.
First create the object, then you can use dynamic names:
var obj = {
check: 'element_id',
element_id: element_id,
}
obj['lang-'+lang_id] = value;
mysql('translations', obj);
You can do this:
var x = {
check: 'element_id',
element_id: element_id
};
x['lang-'+lang_id] = value;
mysql('translations', x);

Categories