Get values from a JavaScript array [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
Say I have an array like:
var someArray = [{"id":1,"mid":"477","mname":"StackOverflow","image":"images/merchants/so.jpg"}, {"id":2,"mid":"478","mname":"Meta","image":"images/merchants/mt.jpg"}]
How do I get say the image string images/merchants/so.jpg for example?

someArray[0].image
or
someArray[0]['image']

Related

how to get value from list javescript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
I'm trying to extract a value with a specified key. below are the data example;
example = [{'a':0,'b':2,'c':4}]
"results I want : 2"
To get the value of b, you would need to use example[0]['b']. You need the [0] since you also have an array enclosing the object.

Can Array.prototype.includes() search for an Array? [duplicate]

This question already has answers here:
Why are two identical objects not equal to each other?
(9 answers)
How to compare arrays in JavaScript?
(55 answers)
Closed 4 years ago.
Consider the following code:
let a = [
["10.237.77.82", "10.237.79.255"],
["10.237.78.2", "230.0.0.1"],
["10.237.78.2", "10.237.79.255"]
]
console.log(a.includes(["10.237.77.82", "10.237.79.255"]))
I was expecting the includes() to find the Array ["10.237.77.82", "10.237.79.255"], why isn't it the case?
Array.prototype.includes() documentation does not seem to suggest specific elements which are "foundable" (although the examples are only with an Integer or a String)

how to access a JSON element when i have a value of another element in the same object? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
Assume that i have this json obj:
var person=[{"name":"joe","age":21,"class":"a"},{"name":"moe","age":22,"class":"b"}];
i want to get the age of joe using given name which is joe.
is there something like this:
var age = person.name['joe'].age
You can use Array.prototype.find to search for a specific property in an array of objects.
var person=[{"name":"joe","age":21,"class":"a"},{"name":"moe","age":22,"class":"b"}];
const joe = person.find(item => item.name === 'joe');
if(joe)
console.log(`Joe is ${joe.age} years old`);

How to access json object from array [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
In my code one of the promise returning the result like this
[{"success":true,"fileName":"./tmp/test.txt"}]
and I want to convert it into Json. how to convert it?
output:-{"success":true,"fileName":"./tmp/test.txt"}
You receive an array containing one object.
Just access array's first element, like :
var array = [{"success":true,"fileName":"./tmp/test.txt"}];
var fileName = array[0].fileName;

node-hashtable - how to get set of all keys? [duplicate]

This question already has answers here:
How to list the properties of a JavaScript object?
(18 answers)
Closed 8 years ago.
In Java it's called the keyset.
https://www.npmjs.org/package/node-hashtable
how come there is no method to return a list of all the keys?
Looking at the source there seems to be a method indexes which returna a list of the keys.

Categories

Resources