This question already has answers here:
Get array of object's keys
(8 answers)
Closed 3 months ago.
Object Im have Object , but how can get keys + value in this (main : , weather : , clouds: , cors : , ... )
Im try use map but can't go next entry
My demo
Sorry im bad english . thanks alot
Please, see here. This is how you get both the key and value of an object.
const foobar = {main: "hello", clouds = "world", cors: 1}
const keypairs = Object.entries(foobar);
//looping
keypairs.map(([key, value]) => { // do something });
just replace value with value.main/ value.weather and it will work
andd one thing cities.map() is not enough you dont need cities.list.map()
good luck!
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 months ago.
So i want to get a value from a json file, but for the life of it, i cant get to understand what to write.
So if someone could help me write this line of code in javascript node, i would be grateful.
I am trying to get the value of "totalCount"
This is from a library called GeoDb just for context.
main.js(NODEjs)
const timezone = require("./TimezonePortugal.json"); //referencing the json
console.log(timezone.metadata); // i dont know what to say afther this
TimezonePortugal.json
{"data":[{"code":"EUR","countryCodes":["PT","YT","IE","AD","IT","AT","RE","AX","BE","BL","SI","SK","SM","CY","DE","LT","LU","LV","MC","ME","MF","EE","MQ","MT","VA","ES","NL","FI","FR","GF","GP","GR","PM"],"symbol":"€"}],"metadata":{"currentOffset":0,"totalCount":1}}
"." means inside.
If you want to get value of totalCount you have to think like this:
timezone.metadata.totalCount = totalCount inside the metada, metadata inside the timezone
For example:
var timezone=
{
"data":[
{
"code":"EUR",
"countryCodes": ["PT","YT",],
"symbol":"€"
}
],
"metadata": {
"currentOffset":0,
"totalCount":1
}
}
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);
This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 7 years ago.
//filejson.json
{
"body":
{
"name":"abc"
}
}
//mainFile.js
var readJson = require("/filejson.json")
var req=clone(readJson.body);
I want to change the name of JSON value without changing .json file. I need to set name value using rewire module. Can you help me how to set key value pair dynamically. Is it possible to set like this. Please help me Thanks in advance.
var readJson = require("/filejson.json")
readJson.name = "defg";
Require just use the file, no changes will occur to the file.
This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 8 years ago.
I need to print all datas in an object in a for loop but I don't know how to access to their index.
For example I have an object like this (this is an example ,but the real one is very big)
a = { name : richard ,last_name : stallman };
I can do this :
cnosole.log(a.name);
cnosole.log(a.last_name);
But the object is too big.How can I do this?
Our you could do it like this:
a = { name : 'richard' ,last_name : 'stallman' };
for( key in a){
console.log(a[key]);
}
You can convert an object to a string with JSON.stringify.
console.log(JSON.stringify(a));
should print something like:
{"name": "richard", "last_name": "stallman"}