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));
Related
This question already has answers here:
One liner to flatten nested object
(19 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed last month.
I have a array as follows:
data = [
{
"data": {
"id":1,
"vol":"0.0"
"details":{
"ABC":"8.30",
"OFG":"13.85",
"SPG":"70.80"
}
}
},
{
"data": {
"id":2,
"vol":"1.0"
"details":{
"ABC":"3.30",
"OFG":"15.85",
"SPG":"70.80"
}
}
}
]
I want to make an arrays from above array such that data inside details object is every element comes outside. So in my final array there will not be details object. I just want to bring all attributes of details object with other object. So my final array will look something like this
data = [
{
"data": {
"id":1,
"vol":"0.0"
"ABC":"8.30",
"OFG":"13.85",
"SPG":"70.80"
}
},
{
"data": {
"id":1,
"vol":"0.0"
"ABC":"8.30",
"OFG":"13.85",
"SPG":"70.80"
}
}
];
How can I do that?
You can use Array#map with object destructuring.
const arr=[{data:{id:1,vol:"0.0",details:{ABC:"8.30",OFG:"13.85",SPG:"70.80"}}},{data:{id:2,vol:"1.0",details:{ABC:"3.30",OFG:"15.85",SPG:"70.80"}}}];
let res = arr.map(({data: {details, ...rest}}) => ({data : {...rest, ...details}}));
console.log(res);
You could make use of the Spread Operator and do something like this:
const builtData = originalData.map(({ data }) => ({
data: {
id: data.id,
vol: data.vol,
...data.details
}
}))
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 1 year ago.
Array looks list :
[
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
]
I have a value i.e Uid 673bjhbjhdcsy, how do I check if that Uid exists in the array and get the whole object associated with the Uid.
You can use find like:
const data = [
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
];
console.log(data.find(x => x.Uid === '673bjhbjhdcsy'));
Reference:
Array.prototype.find()
result = [
{
"Name":"S",
"Level":"1",
"Uid":"huybd776",
"isHuman":false
},
{
"Name":"R",
"Level":"35",
"Uid":"673bjhbjhdcsy",
"isHuman":true
}
].find(item => item.Uid === '673bjhbjhdcsy')
This question already has answers here:
Merge multiple objects inside the same array into one object [duplicate]
(2 answers)
How to concatenate properties from multiple JavaScript objects
(14 answers)
Closed 1 year ago.
I want to convert JSON array to a single object. PFB the details
Array:
[{ "item-A": "value-1" }, { "item-B": "value-2" }]
Expected Result:
{ "item-A": "value-1", "item-B": "value-2" }
I have tried following options but result is not what I was expecting
let json = { ...array };
json = Object.assign({}, array);
json = array.reduce((json, value, key) => { json[key] = value; return json; }, {});
Result:
{"0":{"item-A":"value-1"},"1":{"item-B":"value-2"}}
You can use Object.assign and spread the array
const arr=[{ "item-A": "value-1" }, { "item-B": "value-2" }];
console.log(Object.assign({},...arr));
You can use reduce like how you did it with more attention like this:
let array = [{ "item-A": "value-1" }, { "item-B": "value-2" }];
let object = array.reduce((prev, curr) => ({ ...prev, ...curr }), {});
console.log(object);
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 2 years ago.
This is what my array looks like:
const items = [
{ uuid: '123-1234-567', amountMoney: '20,02' },
{ uuid: '111-111-111', amountMoney: '44.04' }
]
And I have the uuid key in the variable:
const uuid = '111-111-111';
Now based on this uuid, I would like to extract the value from the amountMoney: 44.04.
How do you write this in a nice way in js?
You can use Array.prototype.find:
items.find(item => item.uuid === uuid) // -> found object
Use Array.prototype.find to find the object if the property uuid of the object matches the value of the variable uuid. Before extracting the value for amountMoney check if the object was found.
Example,
const items = [
{ uuid: '123-1234-567', amountMoney: '20,02' },
{ uuid: '111-111-111', amountMoney: '44.04' }
]
const uuid = '111-111-111';
const foundItem = items.find(item => item.uuid === uuid);
if (foundItem) {
console.log(foundItem.amountMoney)
}
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)