This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I am having a hard time trying to accessing any elements in my object.
Find below my code that accesses the object from the localStorage and prints it out on the browser console:
var test = localStorage.getItem('transactionData');
console.log(test);
The above code yields:
[{Amount":"15,000","payersNumber":"070505788","waitersName":"Agnes"}]
When I try to access the element waitersName as seen in the code below:
console.log(">> " +test.waitersName);
It yields:
>> undefined
How do I access the various elements in my object?
test is an array with an object in it, you'll need to access the array item first with test[0] and that will return the object, which will then allow you to access its properties.
The data you retrieve from localstorage is a stringified version of the Javascript array. You first have to parse it using
var array = JSON.parse(test);
Then get your elements from the parsed array.
Related
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 months ago.
I have a script which receives a JSON formatted file as input and allows the user to call out a JSON object name for specific processing. I loop through the JSON file using a foreach() to process the specific items requested by the end user.
In this example The JSON input file has an object array called "socks". The requested JSON object name could be input by the user as "socks" as this would be the object they would like to have the code perform a function on.
I push all of these "socks" to a new array to perform the work on.
//working code
jsonDataIn.socks.forEach(function(s) {
newArray.push({ socks:s })
});
The above code functions as needed because I have hardcoded the array name as 'socks'. I cannot figure out how to assign the array name as a variable to apply the users input.
//I'd tried assigning just the array name as a variable, as well as both the data and array and those both are not valid.
let inputValueFromUser = 'socks';
let arrayNameVar=jsonDataIn.inputValueFromUser;
arrayNameVar.forEach(function(s) {
newArray.push({ [inputValueFromUser]:s })
});
nor this
let inputValueFromUser = 'socks';
let arrayNameVar=inputValueFromUser;
jsonDataIn.arrayNameVar.forEach(function(s) {
newArray.push({ [inputValueFromUser]:s })
});
In the above code the [inputValueFromUser] works for assigning the value to the newArray, but I cannot figure out how to get it to work inline as a form of 'jsonDataIn.inputValueFromUser.forEach()'. I found many examples if I were using multiple arrays in a forEach loop, but not how to pass the 'inputValueFromUser' as an array name.
I know it has to be a simple solution that I'm missing. Any assistance is appreciated.
All of the failed code that I've tried to make work has resulted in "TypeError: Cannot read properties of undefined (reading 'forEach')"
This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 1 year ago.
{
pet: {
"0.628": 92694.5,
"8739.836": 96391.94
},
try: {
//same
}
}
When I specify the key, I get the values but i am trying to read all the values without knowing the keys. I have even tried regex, but nothing seems to be working. As you can see i am fairly new. So sorry if this was a stupid question.
console.log(data.pet) // Gives [Object Object]
console.log(data.pet["0.628"])//Gives the value
console.log(data.pet[0])//Gives undefined
I don't see in what context you'd want to access a json object without knowing the keys.
but what you can do is to parse the json file into a javascript object, and call Object.keys() to get the keys of that object
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.
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
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Hello I am doing a school assignment my main problem is as follows.
var objectQueue = {
customers:[
{name:"Phil", order:"coffee"},
{name:"Sandy", order:"coffee"},
{name:"Enrique", order:"sandwich"},
{name:"Joe", order:"coffee"},
{name:"Alex", order:"muffin"},
{name:"Zoe", order:"chili"},
{name:"Bahamut", order:"sandwich"},
{name:"Rydia", order:"timbits"}
]
};
I have this object, I need to know how to access each customer's order through a for loop. I can't get the loop to read each person's order. What would be the right way to do this?
This is where I am currently:
objectQueue[x]order
Assuming x is a counter:
objectQueue.customers[x].order
objectQueue has a property named customers, to access a simple property on a Javascript object, you can just use its name:
objectQueue.customers
Then, customers has a array of objects. To access elements in a array, we use its index:
customers[0]
Since the elements in the list are maps/objects, we can access them via properties as well:
customers[0].name
Putting this all together we get:
objectQueue.customers[0].name
Almost everything in Javascript is an object, so it's a little misleading to differentiate between arrays and objects (since arrays ARE objects), but I'm assuming you can dig into those details later if you're interested. In the meantime, this should get you going.
You first need to access the length of the customers and use that as your loop count, from there you can use 'i' your counter to access properties
for (i=0; i<objectQueue.customers.length; i++){
console.log(objectQueue.customers[i]);
console.log(objectQueue.customers[i].name);
console.log(objectQueue.customers[i].order);
}