How to correctly get JSON value? [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I have this json array
var currencyformats =
{"USD":[
{'symbol':'$', 'left':true}
],
"UAH":[
{'symbol':'₴', 'left':true}
],
"EUR":[
{'symbol':'€', 'left':false}
]
};
How retreive '₴' ?
I tried this (in cookie "to" I've "UAH")
currencyformats[$.cookie("to")].symbol
but I've obtained undefined

The problem is that under each country code, you've defined an array with a single object. That means that after you access the country code, you'll need to also access the first index in the array.
So, assuming the cookie is set to the value that you're expecting:
currencyformats[$.cookie("to")][0].symbol;

Related

Read nested value from json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
On the image You can see what I want to read
console.log("exact val = " + json._array.loftybot.spells.name)
and it gives [TypeError: Cannot read property 'spells' of undefined]
You can see in terminal structure of json
How to get into these values?
I want save values from json to array const json = new ObservableArray
And then use all of these values in my program
How about json._array[0].loftybot.spells.name? with an index 0 on your _array and go further on nested json. SEE at MDN

How to remove an array element from local storage? [duplicate]

This question already has answers here:
Remove array item from localstorage
(4 answers)
Closed 4 years ago.
I have an array of contacts in local storrage, and I need to remove, for example, the first element. How better to do? Is this expression correct?
localStorage.removeItem("allContacts"[0]);
localStorage contains string values. If you stringifyed an array and put it into localStorage, then you'll need to parse the array, delete the element you want, and then set the localStorage property again:
const allContacts = JSON.parse(localStorage.allContacts);
allContacts.shift();
localStorage.allContacts = JSON.stringify(allContacts);

How to read Object? [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
Say i have object like
var a = {"user":
{'average':
{'score':4
}
}
}
How can I read object value using its keys
Say I have user Object with me and have key "average.score" can I get the value directly?
a.user["average.score"];
//Coming as undefined
a.user["average"]["score"]
// Working as expected : 4
I have the key of "average.score" all together with me want to get the value of score how can I do it directly without splitting the key.
Use a.user["average"].score
var a = {"user":
{'average':
{'score':4
}
}
}
console.log(a.user["average"].score);

How to access Json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I am trying to read from a JSON array with a nested array which has its value name spaced out. So I get an error whenever I run the code.
var error = [
{
"LessonName":"Understanding Multiplication",
"LessonID":"13343",
"no of questions":[{"Locked":"31","Unlocked":5}]
},
{
"LessonName":"Finding Unknown Values ",
"LessonID":"13424",
"no of questions":[{"Locked":"34","Unlocked":5}]
}
]
function jsd(){
document.write(error[0].LessonName);
document.write(error[0].'no of questions'[0].Locked);
}
document.write(error[0]."no of questions"[0].Locked); Doesn't seem to display.
You may use a property accessor with brackets for the string.
error[0]['no of questions'][0].Locked
You must use this syntax for strings with spaces.
document.write(error[0]['no of questions'][0].Locked);

Javascript, cannot access JSON property [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I have a json string received from websocket :
{
"type":"newLoan",
"cartItems":{
"numberOfItems":null,
"bookList":[
{
"id":"1",
"title":"The Count of Monte Cristo",
"author":"Alexandre Dumas ",
"genre":"Comedy",
"returnDate":"January 15"
}
]
}
}
This result is shown after console.log(receivedMessage), but when I try to access type property by console.log(receivedMessage["type"]) it gives me undefined.
Still the same with console.log(receivedMessage.type).
How could I access the type property?
There is probably an issue with the headers that are being sent along with your string, if the content type isn't set to application/json or the javascript equivalent then it will be treated as a string and no a json object
try:
JSON.parse(receivedMessage).type

Categories

Resources