Check object key has empty value [duplicate] - javascript

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));

Related

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

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

concatenating two object keys in JSON [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
console.log(a) ; // console window result= 1
console.log(b);// console window result= 2
var c = {a : b};// any recommendations here?
var d = JSON.stringify(c);
d = encodeURIComponent(d);
I need final result of d = {1:2};
You can use computed property
var c = {[a] : b};

How can I create a new object from existing values? [duplicate]

This question already has answers here:
create object using variables for property name [duplicate]
(4 answers)
Closed 8 years ago.
I have
object.name = 'foo';
var value = 'bar';
...
var params = { object.name : 1, value : value};
I want a result equivalent to
var params = { foo : 1, value : bar };
what can I do?
Objects are sometimes called associative arrays, since each property is associated with a `string value that can be used to access it.
Try using [] to set object property -
object.name = 'foo';
var value = 'bar';
var params = { value:value};
params[ object.name] = 1;
Output:- {value: "bar", foo: 1}

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;

Categories