"indefined selection console.log(data[])" [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 can recover my api but I cannot recover firstname in my object people, do you have a solution?
here my code for select l'object people :
console.log(data['people']);
and i want select firstname in my object people

Your people object is an array so, to get the firstname value you need to either loop in this array (with foreach or map) and then use .firstname, or you can just specify an entry
so you can do :
data['people'][0].firstname
or
data['people'].forEach(element => {
element.firstname
});

can you loop the array by using map.
data['people'].map((item, index) => {
console.log(item.firstname)
})

Related

how to create an array of values from an existing array in Javascript [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 5 months ago.
I have an array which has many objects, I want to make an array of tag values so I can display the non repeated tags in the frontend.
This is the array, and from which I want to create the new array which will include all the tag values.
Can you please share with me a solution or maybe a way to solve this ? Thanks
You can use Array#map to get all the tag property values, and then create a Set from that to get the unique ones.
const tags = [...new Set(yourObj.nodes.map(x => x.tag))];

Take a subset from a complex object in JS [duplicate]

This question already has answers here:
How to map more than one property from an array of objects [duplicate]
(7 answers)
Closed 1 year ago.
How can I take (in JS) all the values inside this nested object except for the last two elements (scenario and total). here an image of the object I have:
I need an array equal to this one, but without "scenario" and "total" in the 3 nested objects inside.
Use map function
array.map(item => {
delete item.scenario;
delete item.total;
return item;
});

How to acces a nested json value when it has no key? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
How can you access a nested json value when the value has no key?
im trying to access the "2019-03-19T22:57:47.972Z" value of this json object:
var json = {"metaData":[{"name":"ACTION_NAME"},{"name":"SENT_RECV_TIME"}],"rows":[["SI_OA_CTPParameters","2019-03-20T06:20:45.704Z"],["SI_OA_CTPParameters","2019-03-21T06:04:08.313Z"],["SI_OA_CTPParameters","2019-03-21T06:01:14.412Z"],["SI_OA_CTPParameters","2019-03-20T06:59:54.875Z"],["SI_OA_CTPParameters","2019-03-20T20:32:50.975Z"],["SI_OA_CloudDataAddress","2019-03-19T22:57:47.972Z"],["SI_OA_CloudDataAddress","2019-03-19T22:56:52.115Z"],["SI_OA_CloudDataAddress","2019-03-19T22:54:28.196Z"] ......
what is can reach rigth now is just json.rows[0], which return:
["SI_OA_CTPParameters","2019-03-21T06:04:08.313Z"]
I tried json.rows[0].[1] but this does not work.
I just need the second value "2019-03-21T06:04:08.313Z", how can I acces it?
You can access nested values using json.rows[0][1], like this:
var json = {"metaData":[{"name":"ACTION_NAME"},{"name":"SENT_RECV_TIME"}],"rows":[["SI_OA_CTPParameters","2019-03-20T06:20:45.704Z"],["SI_OA_CTPParameters","2019-03-21T06:04:08.313Z"],["SI_OA_CTPParameters","2019-03-21T06:01:14.412Z"],["SI_OA_CTPParameters","2019-03-20T06:59:54.875Z"],["SI_OA_CTPParameters","2019-03-20T20:32:50.975Z"],["SI_OA_CloudDataAddress","2019-03-19T22:57:47.972Z"],["SI_OA_CloudDataAddress","2019-03-19T22:56:52.115Z"],["SI_OA_CloudDataAddress","2019-03-19T22:54:28.196Z"]]};
console.log(json.rows[0][1]);
json.rows[0] returns an array. Lets call this array a.
You can reference the elements of an array by their index, thus: a[1] will yield your requested element.
However, renaming the array is not convenient, you can simply substitute the original statement back into the a; thus json.rows[0][1] will work.

Access to json variable (node.js) [duplicate]

This question already has answers here:
Iterate through object properties
(31 answers)
How to parse JSON data when the property name is not known in advance?
(2 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
How can I get the value "success" if the value that stands in place "Everlasting hair" is changing?
{
'Everlasting hair' :
{ succes : true,
volume:'12'
}
}
You can discover the property names in the object via for-in or Object.keys. Object.keys, for instance, will give you an array of the object's own, enumerable properties. If you know for sure there will only be one, then:
var success = yourObject[Object.keys(yourObject)[0]].succes;
// Another s here? It's missing in the question -----------^
On cutting-edge JavaScript engines with Object.values (new in ES2017, but polyfillable), as Keith points out if you don't need the name you can use Object.values instead:
var success = Object.values(yourObject)[0].succes;
// Another s here? It's missing in the question -^

Underscore.js get value of key object in array of object [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
i have to get the value of wave1 and wave2 from array by using underscore.js
array =[{"id":1,"name":"Monoprix", "pdv":16,"graph":[{"wave1":22,"wave2":11}]} ;
i try
$scope.wave1 =array.graph.wave1 ;
console.log($scope.wave1) ;
console log = Unidentified
can you help me !
your code is wrong, you miss ] after element list.
your array :
var array =[{"id":1,"name":"Monoprix", "pdv":16,"graph":[{"wave1":22,"wave2":11}]}];
and to get some data you must first select array index, like this:
array[0].graph

Categories