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

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 -^

Related

How do I convert a string into a property in javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
Now I am sending length as a string as a function parameter.
function sample(operation){
var str="hello";
console.log(str.operation);
}
sample("length");
I am not allowed to change the way I am sending length(has to be a string).What can I do for this function to give me the expected output?
If you just want to access the property, use bracket notation for your property accessor:
console.log(str[operation])

Unable to get object data when used multiple-argument function [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I am unable to get the data of an object
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a.val[0])
}
fun('ab','ef')
It should output 'cd' but it is giving out error in the console
any idea how do i fix this...
Use bracket notation like so:
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a[val[0]])
}
fun('ab','ef')
Your code was trying to get the property named val in a (doesn't exist), then get the first character/item of that value (trying to do this to undefined causes the error).

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.

How do I iterate over a dynamic keyed object? [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 6 years ago.
I have a hashed object where the keys are added dynamically through user selections.
I want to iterate over it and extract the values similar to the way I would do if it was simply an array: selections.map(cart => /*do stuff*/).
How can I achieve this?
Use Object.keys()
The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
var array = Object.keys(selections).map(k => selections[k]);
// get all values from the object

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