This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 3 years ago.
I have this Array Object that looks like this:
let arr = [
{Name: "sub", Value: "ababbnnn"}
]
I'm trying to access the value of the Name custom:network meaning I want to output this: abcdef1233bfgh. So far I have this loop but I wonder if there may be a cleaner way. Thanks a lot in advance. Here's my code:
You can use the find method:
const value = arr.find(item => item.Name === "custom:network").Value
To cover the case where no item is returned by find, you can use the following approach:
const value = (arr.find(item => item.Name === "custom:network") || {}).Value
Related
This question already has answers here:
How to map more than one property from an array of objects [duplicate]
(7 answers)
Closed 2 years ago.
I have an array of objects like this:
var array = [{date:'01/01/2017',value1:200,value2:300,value3:400}, {date:'02/01/2017',value1:220,value2:330,value3:430},{date:'03/01/2017',value1:250,value2:330,value3:420}]
I am trying to get the subset of the array objects like below in Javascript: I also need to add a 10 to value2.
var arrayOne = [{date:'01/01/2017',value2:300}, {date:'02/01/2017',value2:330},{date:'03/01/2017',value2:330}]
Any suggestions would be greatly appreciated.
You could use map to iterate and select
var array = [{date:'01/01/2017',value1:200,value2:300,value3:400}, {date:'02/01/2017',value1:220,value2:330,value3:430},{date:'03/01/2017',value1:250,value2:330,value3:420}]
console.log(array.map(({ date, value2 }) => ({ date, value2: value2 + 10 })))
This question already has answers here:
Sort JavaScript object by key
(37 answers)
Closed 2 years ago.
I have an object {5:"James",1:"John", 3:"Jade"}. When reading its entries it should read it in below way {1:"john",3:"Jade",5:"James"}, without modifying the actual object.
Just sort the object
const obj = {5:"James",1:"John", 3:"Jade"};
const result = Object.fromEntries( Object.keys(obj).sort().map((key) => {
return [key, obj[key]];
}));
console.log(result);
This question already has answers here:
Access object child properties using a dot notation string [duplicate]
(13 answers)
Closed 3 years ago.
I know I'm missing something obvious here but say I have a JSON object that looks like this:
testObj = {
levelOne: {
levelTwo: []
}
}
I also have a string value:
var prop = 'levelOne.levelTwo';
I'm trying to determine if there's any way to basically do something like this:
var x = testObj[prop];
That doesn't work, but is there any way to do the equivalent?
There's no trivial way (e.g. testObj[prop]) of doing this, but the reduce function is well suited:
let nestedProp = (obj, path) =>
path.split('.').reduce((obj, prop) => obj[prop], obj);
let x = nestedProp({levelOne: {levelTwo: [5]}}, 'levelOne.levelTwo');
console.log(x);
You can use dynamic keys to access properties in an object but not multiple levels down.
i.e. You can do const a = testObject["levelOne"] but not what you tried. (Docs)
There are however helper libs that have functions to do this. One example is lodash.get function
This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 3 years ago.
I have a somewhat strange question,
I would like to know if it is possible to use a string as an entire reference to get a value from an object within an array.
This is my array:
const myArray = [
{name: 'element1', id: 'elementid1'},
{name: 'element2', id: 'elementid2'}
];
where myArray[0]["name"] returns: 'element1'
Would it be possible to have this entire reference: myArray[0]["name"] as a string: 'myArray[0]["name"]' and use it to reference to this value.
So this: getViaString returns 'element1' with the following set up:
const getViaString = 'myArray[0]["name"]';
I have set up this fiddle as it probably explains better what I am trying to do:
jsfiddle
Thanks.
You could potentially use eval() - not recommended.
const getViaString = eval("myArray[0]['name']");
yes you can do it with
const getViaString = eval('myArray[0]["name"]');
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
How to do i get the id value and brandName value
[{"id":24,"brandName":"BMW"},{"id":25,"brandName":"Mercedes Benz"}]
Not sure what you're trying to do as your question is very vague. But if you want to iterate over each element and do something with the values you could do something like this:
let items = [{"id":24,"brandName":"BMW"},{"id":25,"brandName":"Mercedes Benz"}];
items.forEach(elem => {
const {id, brandName} = elem;
console.log(`ID: ${id}. Brand Name: ${brandName}.`);
})