Get key from JSON object [duplicate] - javascript

This question already has answers here:
Javascript get object key name
(8 answers)
Closed 5 years ago.
I'm using the flat-cache NPM package and I'm currently blocked because I can't recover the key from the data I'm caching. It must be very simple, but I'm starting with the JS and I'm pulling my hair out on this problem.
Simple example of code :
main.js
var flatCache = require('flat-cache')
flatCache.setKey('d86f003c-bf0a-4b08-9744-1081c78ece9d', {"creation":"2018/02/20", "link":"https://www.npmjs.com/package/uuid","comment":"UUID", "tags":["NPM", "UUID"]});
var a = flatCache.all();
console.log(a);
Example of data from console :
{
"d86f003c-bf0a-4b08-9744-1081c78ece9d": {
"date":"20180220",
"comment":"Hello world",
"tags":[
"hello",
"worlds"
]
}
}
What would be the procedure to follow to retrieve the key : d86f003c-bf0a-4b08-9744-1081c78ece9d ?
Thank you in advance for your answer !

Use Object.keys method.
In your case:
// `a` is defined somewhere there
...
Object.keys(a); // an array of object keys - but only the first level
console.log(Object.keys(a)[0]); // should log `d86f003c-bf0a-4b08-9744-1081c78ece9d`
for further reference - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Related

ReactJS : How can get value in object [duplicate]

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!

Get value of json file in javascript(nodejs) [duplicate]

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

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 set json value by using rewire module in node.js [duplicate]

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.

How to show an object value in for loop in java script [duplicate]

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"}

Categories

Resources