Using dot notation with variable to get object value in javascript [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 4 years ago.
I have a config object like this
{ config: {
params: {
football: {
url: ''
},
soccer: {
url: ''
}
}
}
I simply need to get at the football or soccer url value using a variable, so something like
let sport = 'soccer';
let path = config.params.`sport`.url;
I've tried bracket notation like config.params[sport] and the eval function but neither seem to do the trick. I'm sure I'm misnaming what I'm trying to do which is likely why I can't seem to find an answer.
thanks

This should give you an idea.
const sport = 'soccer'
const data = {
config: {
params: {
football: {
url: '1'
},
soccer: {
url: '2'
}
}
}
}
console.log(data.config.params[sport].url)

Related

How to get array of same keys from an array of object in Javascript? [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
sorry before if this is an duplicate or have similarity with other post, but I cannot find problem with the same context as I have.
So I have an array of object, something like this
const queryResult = [
{ token: 'c5WKMXW8QdCFUg4q8ica' },
{ token: 'Othertoken' },
{ token: 'moreothertokens'},
]
So, i want to merge those 3 tokens into an array, something like this
['c5WKMXW8QdCFUg4q8ica', 'Othertoken', 'moreothertokens']
I am using forEach for the solution at the moment. But is there any shortcut or cleaner code?
Thank you.
Try this:
const tokens = queryResult.map(x => x.token);
If you have only one property in the objects, you could take a flat map approach with Object.values as callback.
const
queryResult = [{ token: 'c5WKMXW8QdCFUg4q8ica' }, { token: 'Othertoken' }, { token: 'moreothertokens'}],
tokens = queryResult.flatMap(Object.values);
console.log(tokens);
You could use map with object destructing
const queryResult = [
{ token: "c5WKMXW8QdCFUg4q8ica" },
{ token: "Othertoken" },
{ token: "moreothertokens" },
]
const tokens = queryResult.map(({ token }) => token)
console.log(tokens)

Checking if an array of objects contains value without looping? [duplicate]

This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Closed 3 years ago.
Right now I have an array that looks like this
const array = [
{
value: 'received',
title: 'Hjá Birgja',
},
{
value: 'pending',
title: 'Yfirstandandi',
},
{
value: 'processing',
title: 'Í vinnslu',
},
]
and I would like this to return true
if(array.includes('processing'){
// do something
}
not exactly what you wanted since i'm not sure if you only wanted to search the value key but here's a solution
if (array.find(i => i.value === 'processing')) {
// do something
}

What does this variable declaration form mean? [duplicate]

This question already has answers here:
Javascript object bracket notation ({ Navigation } =) on left side of assign
(5 answers)
Closed 3 years ago.
It is that situation, when you get some code, which works, but you don't know how.
What does this declaration method do?
const { actions: { createRole, updateRole } = {} } = props;
The code uses destructing for an nested object. The following example might help for understanding this new JavaScript syntax (has been introduced with ES6):
const user = {
id: 339,
name: 'Fred',
age: 42,
education: {
degree: 'Masters'
}
};
const {education: {degree}} = user;
console.log(degree); //prints: Masters
I would recommend the following resource for further examples:
https://medium.com/#pyrolistical/destructuring-nested-objects-9dabdd01a3b8

Map a string array to a json object in JavaScript [duplicate]

This question already has answers here:
Javascript: How to Get Object property using Array of string? [duplicate]
(7 answers)
Closed 3 years ago.
I have a config-file:
{
"permission": {
"users": {
"image": {
"data": "example"
}
}
}
}
And an array with a called path like this:
path = ['users', 'image']
How can I get the data?
First try:
config.permission.path[0].path[1];
Second try:
switch (requestedPath[2]) {
case 'users':
switch (requestedPath[3]) {
case 'image':
mydata = config.permission.users["/image"]
}
}
This will work, but is there a better way?
You need a bracket as property accessor for the object, because you take a variable as key.
config.permission[path[0]][path[1]];
For a more dynamic approach, you could reduce the given data with a default object for not fiund properies.
const getV = (object, path) => path.reduce((result, key) => (result || {})[key], object);
var config = { permission: { users: { image: { data: 'example' } } } },
path = ['users', 'image'];
console.log(getV(config.permission, path));

JavaScript Object property always returns undefined [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
Tell me what I am missing here. I have the follow javascript object.
[ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD',
handle: '123',
userId: 'ABC123'} ]
When I do the following
success: function (registration) {
console.log(registration);
console.log(registration.handle);
Console log writes out the object as defined above. However when I do registration.handle I get an error saying "undefined." If registration is the above object why does registration.handle not work?
what am I missing?
You have an array containing an object. The properties you are trying to access are members of the object, not the array.
You must first get a reference to the object before you access its properties.
registration[0].handle
Try this
var registration=[ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'} ]
alert(registration[0].handle)
DEMO
You are accessing the member of an object.
Do it like this way
success: function(registration) {
$.each(registration, function(index, data) {
var handle = data.handle;
console.log('id is getting now ' + handle);
});
}
Yes you first need to access array element then you can find object
console.log(registration[0].handle);
it is because you are having array so to access it try
registration[0].handle
EXAMPLE
CASE 1
registration = [ { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'} ];
console.log(registration[0].handle);
CASE 2
registration = { id: '16B0C2FC-A008-4E8A-849B-DB1251C8CABD', handle: '123', userId: 'ABC123'};
console.log(registration.handle);

Categories

Resources