how to get value from list javescript [duplicate] - javascript

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.

Related

how to access dynamic key and value of json object [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Access nested JSON object without specifying key in JavaScript
(1 answer)
How do I loop through or enumerate a JavaScript object?
(48 answers)
How to access a javascript object value without knowing the key [duplicate]
(2 answers)
Closed 21 days ago.
"value": "{"urla":{
"url1":"url2",
"url3":"url4",....}}"
how can i access urla, url1 and url2...from json object. how to access dynamic key and value of json object using value.
This urla, url1, url2 , url3 values are dynamic.
we need to access them.
in what way can we access each of them.

Reference to variable versus "actual" variable (PHP vs Javascript) [duplicate]

This question already has answers here:
PHP foreach change original array values [duplicate]
(5 answers)
PHP Pass by reference in foreach [duplicate]
(9 answers)
Closed 4 years ago.
In the Javascript code:
var people = [{name:"john",age:20},{name:"bob",age:30},{name:"kate",age:40}];
people.forEach(function(person,i){
person.isHuman = true;
})
console.log(people) would give
[{name:"john",age:20,isHuman:true},{name:"bob",age:30,isHuman:true},{name:"kate",age:40,isHuman:true}]
However, the same code in PHP
$people = [["name"=>"john","age"=>20],["name"=>"bob","age"=>30],["name"=>"kate","age"=>40]];
foreach($people as $i=>$person){
$person['isHuman']=true;
}
var_dump($people) would give
[["name"=>"john","age"=>20],["name"=>"bob","age"=>30],["name"=>"kate","age"=>40]]
The new "isHuman" property is not added to the original array of objects.
In the PHP case, I know you can do "$people[$i]['isHuman']=true" to alter the object in the original array. But is there a way to do "$person['isHuman']=true" and have that change reflected in the original array?
And my second question is why/for what reasons is this behavior different between Javascript and PHP?

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;

Get values from a JavaScript 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.
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']

test for a propertie in JSON project to be empty? [duplicate]

This question already has answers here:
Testing for an empty array object in JSON with jQuery
(5 answers)
Closed 8 years ago.
I have the following JSON object that is passed to one of my functions …
{"action":"setProjectAll", "application":{"proId":"new","zoomPosition":35,"scrollerPosition":0,"language":"en","timelinePosition":0}, "clips":[]}
How can I test this object for the propertie "clip" to be "[]" (empty)?
Right now in the example above it is empty, however since the object is dynamic I need to test for that property if it contains values or not.
How can that be done?
thank you
How about
if (x.clips && x.clips.length === 0)

Categories

Resources