This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
I'm trying to access the properties of easeSegmentNameMap using:
easeSegmentNameMap["EMAIL_ENGAGED"].text
I'm getting undefined, it works if I use:
easeSegmentNameMap["EMAIL_ENGAGED"].name
var easeSegmentNameMap = {
"EMAIL_ENGAGED": {
name: "emailEngaged"
},
"EMAIL_INACTIVE": {
name: "emailInactive"
},
"LIKELY_TO_THRIVE": {
name: "likelyToThrive"
},
"MOST_VALUABLE_SUBSCRIBERS": {
name: "mostValuableSubscribers"
},
"NEARLY_INACTIVE": {
name: "nearlyInActive"
},
"NEVER_ACTIVATED": {
name: "neverActivated"
},
"QUESTION_MARKS": {
name: "questionMarks"
}
};
var text = "name";
alert(easeSegmentNameMap["EMAIL_ENGAGED"].text);
Output:
undefined
What am I doing wrong?
Try this:
alert(easeSegmentNameMap["EMAIL_ENGAGED"][text]);
text is a variable and has to be interpolated.
You can't access
var text="name";
alert(easeSegmentNameMap["EMAIL_ENGAGED"].text);
the text is still the property, in this case, it's not the variable.
If you wanted to access the variable you should access like this:
alert(easeSegmentNameMap["EMAIL_ENGAGED"][text]);
Just need to use [] bracket notation. whenever you need to access any object property using variable you need to use [] notation.
var easeSegmentNameMap = {
"EMAIL_ENGAGED": {
name: "emailEngaged"
},
"EMAIL_INACTIVE": {
name: "emailInactive"
},
"LIKELY_TO_THRIVE": {
name: "likelyToThrive"
},
"MOST_VALUABLE_SUBSCRIBERS": {
name: "mostValuableSubscribers"
},
"NEARLY_INACTIVE": {
name: "nearlyInActive"
},
"NEVER_ACTIVATED": {
name: "neverActivated"
},
"QUESTION_MARKS": {
name: "questionMarks"
}
};
var text = "name";
console.log(easeSegmentNameMap["EMAIL_ENGAGED"][text]);
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
Want to know how we can access the items in an object when passed to a function :
const sortArrayObj = (arrayObj, field) => {
//How to access "arrayObj[0].field" ???
// Tried to do console.log(`${arrayObj[0]}`) and it gave output as [Object object]
//Now how can i access "name" and "date" from it using the "field" variable ?
};
const arr = [
{ name: "A", date: "13-12-1996" },
{ name: "G", date: "03-02-2020" },
{ name: "C", date: "09-05-2021" },
];
sortArrayObj(arr,"date")
Try in the following way:
const sortArrayObj = (arrayObj, field) => {
// How to access "arrayObj[0].field" ???
arrayObj[0][field];
};
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
How will you access symbol property here in JS
console.log(hi[0]) outputs { symbol: "asdf" , name:"adad"}
but
console.log(hi[0].symbol) throws the error that symbol is undefined
hi is an array. First you need to access the object so hi[0] will provide the first object. Then access the object using the key which is 0 like below
const hi = [{
0: {
symbol: "asdf",
name: "adad"
}
}];
console.log(hi[0][0].symbol)
hi variable is an Array so you have to access the first element of the array first, then the Object property:
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
console.log(hi[0][0].symbol)
Like this?
console.log(hi[0][0].symbol)
hi = [ { 0: { symbol: "asdf" , name:"adad"} }]
hi[0][0].symbol is right.
console.log(hi[0]) => 0: {symbol: "asdf", name: "adad"}
console.log(hi[0][0]) => {symbol: "asdf", name: "adad"}
console.log(hi[0][0].symbol) => "asdf"
This question already has answers here:
Check if a value exists in an Array object in JavaScript or Angular
(4 answers)
Closed 3 years ago.
I want to return true if a nested array contains a particular value
In this example I'm trying to see if the users array has the current users id but I get the object instead of true
var currentUserId ="MBsCLlPbilRr26Jpz5oxhMULRvC2"
var users = [
{
id: "MBsCLlPbilRr26Jpz5oxhMULRvC2",
name: "Dennis",
url: undefined,
},
{
id: "CLlPbhMULRvC2jnjnDe",
name: "Dennis",
url: undefined,
},
]
console.log(users.find(user=>user.id === currentUserId))
The problem is you are using .find() instead of .some(). Try the following:
var currentUserId ="MBsCLlPbilRr26Jpz5oxhMULRvC2"
var users = [
{
id: "MBsCLlPbilRr26Jpz5oxhMULRvC2",
name: "Dennis",
url: undefined,
},
{
id: "CLlPbhMULRvC2jnjnDe",
name: "Dennis",
url: undefined,
},
]
console.log(users.some(user=>user.id === currentUserId))
The difference is in the output. .find() will return the value, .some() will return a boolean.
This question already has answers here:
Find by key deep in a nested array
(21 answers)
Closed 5 years ago.
I have and multidimensional object that looks like this:
obj = {
'someString': {
name: 'John',
page: 'some url',
number: 4
},
'someString2': {
name: 'Bill',
page: 'some url',
number: 7
}
}
How do I find the first level key (in this case "someString2") where "number" is equal to 7?
The number is always unique and is the only thing I know beforehand.
Here you go. Using Array.find function to look for the appropriate key.
const numberToLookFor = 7;
const data = {
someString: {
name: 'John',
page: 'some url',
number: 4,
},
someString2: {
name: 'Bill',
page: 'some url',
number: 7,
},
};
const myKey = Object.keys(data).find(x => data[x].number === numberToLookFor);
console.log(myKey);
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I need to traverse a JavaScript object with a stored string.
Sample string
var x = "Desserts"
Sample Object
{
"dataset":
{
"Categories" :
[
{
"Desserts" :
[
"Sweets","Ice Creams","Pastry"
]
} ,
{
"Juices and Beverages" :
[
"Cold","Hot","Fresh","Sodas"
]
}
}
}
If I traverse the object as dataset.Categories.x, it doesn't work[returns undefined] . How can I do this?
You should use dataset.Categories[0][x] instead of dataset.Categories[0].x
Take a look at : dot notation or the bracket notation
var x = "Desserts",
data = {
"dataset": {
"Categories": [{
"Desserts": ["Sweets", "Ice Creams", "Pastry"]
}, {
"Juices and Beverages": ["Cold", "Hot", "Fresh", "Sodas"]
}]
}
}
alert(data.dataset.Categories[0][x]);
var obj = {
"dataset": {
"Categories": [{
"Desserts": ["Sweets", "Ice Creams", "Pastry"]
}, {
"Juices and Beverages": ["Cold", "Hot", "Fresh", "Sodas"]
}]
}
}
var x = 'Desserts';
var val = obj.dataset.Categories[0][x];
console.log(val);
JSFIDDLE.