Getting an object with a variable. -javascript- [duplicate] - javascript

This question already has answers here:
How to use variables in dot notation like square bracket notation
(6 answers)
Closed 8 years ago.
var i = "test";
var test1 = {
test: 3,
b: 3
};
console.log(test1.i);
Sorry if it's a simple answer, I am still learning.
var i is looping to something different every few seconds and var i will always be something on test1.

If you are trying to retrieve a property of an object you can use the . notation, or the [] notation
var test1 = {
test: 3,
b: 3
};
Using . notation
test1.test; // -> returns 3
Using [] notation
var propertyName = 'test'
test1[propertyName]; // -> returns 3

Do it this way:
var test1 = {
test : 3
};
console.log(test1[i])

Related

Spread operator with bracket notation [duplicate]

This question already has answers here:
Object destructuring with property names that are not valid variable names
(2 answers)
Closed 3 years ago.
The following works fine:
const o = {one:1,two:2,three:3};
const {one,...others}=o;//one=1, others={two:2,three:3}
But how would I do the following:
var o = {['this-is-one']:1,two:2,three:3};
var {['this-is-one'],...others}=o;
Currently that gives me a SyntaxError: Unexpected token ','
I suspect it would not work because this-is-one would be invalid for a constant name (it only works for property values).
You need a renaming of the variable name, because the the given key is not a valid variable name.
var o = { 'this-is-one': 1, two: 2, three: 3 },
{ 'this-is-one': thisIsOne, ...others } = o;
console.log(thisIsOne);
console.log(others);

Weird javascript code convention with "const" [duplicate]

This question already has answers here:
beginner's: const definition in Redux confusing
(1 answer)
What does curly brackets in the `var { ... } = ...` statements do?
(4 answers)
Closed 5 years ago.
What is this code convention in javascript?
const { navigate } = //whatever
As in, what sense does it make. I saw it in RNs React navigation
https://reactnavigation.org/docs/intro/
It's named destructuring. When you have an object and you want to take only a property of that object, you can get only it by using that convention.
let fullName = {
first: 'John',
last: 'Smith'
}
const { first } = fullName;
You can take a look here for more info's
http://wesbos.com/destructuring-renaming/
It's called destructuring
Example:
var myObject = {a: "what", b: "ever"};
const {a} = myObject;
console.log(a); // will give "what"

Undefined when referencing an object via another object in JavaScript [duplicate]

This question already has answers here:
JavaScript - Why can't I call a variable "name"?
(2 answers)
Closed 5 years ago.
My question is pretty simple. I'm creating two objects. The second object is referencing an object inside the first object.
var me = {
name: {
first: "justin"
}
};
var name = me.name;
console.log(me.name.first); // "justin"
console.log(name.first); // undefined
Why am I getting undefined in my second console log? Shouldn't I get "justin" instead?
You need to use another name. There is a name variable which is global.
var me = {
name: {
first: "justin"
}
};
var anotherName = me.name;
console.log(me.name.first);
console.log(anotherName.first);

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?

Categories