Javascript - retrieve object property path [duplicate] - javascript

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;

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

Accessing an element of a nested object using a variable for the key? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 years ago.
Silly example:
<script>
var a = {
'b' : {
'c' : "success!!"
}
};
var d = 'b.c';
</script>
How could I access success!! if I can't go for the obvious solution a.b.c or a['b']['c'], but instead have to use d? I tried a[d], which doesn't seem to do the trick. I also tried to fiddle with eval(). Is this even possible?
If it's really necessary to have the keys in a string separated with a dot, I would use split and reduce:
var success = d.split(".").reduce(function (obj, key) {
return obj[key];
}, a);
Try splitting
var a = {
'b' : {
'c' : "success!!"
}
};
var d = 'b.c';
var splat = d.split('.');
console.log(a[splat[0]][splat[1]]);

Using a string to return a variable [duplicate]

This question already has answers here:
How to find JavaScript variable by its name
(7 answers)
Closed 8 years ago.
I'm trying to find a way of using a string to refer to a variable.
Here is an example using jQuery:
var array = [1,2,3];
$('.array').click(function(){
var foo = $(this).attr('class');
return foo[1];
});
I want this to return the number '2' -
but as foo is a string it will return the sub string 'r'.
edit - the answer I was looking for was:
var array = [1,2,3];
$('.array').click(function(){
var foo = $(this).attr('class');
return eval(foo)[1];
});
I don't know if this is quite what you mean, but a Javascript object can do this.:
foo = {}; // create object
foo["string"] = [1,2,3]; // now the list [1,2,3] can be referenced
// by foo.string, or foo["string"]
console.log(foo["string"][1]); // Output with string.
Is that what you mean?

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