How to get the keyword of a json varible? [duplicate] - javascript

This question already has answers here:
How to get all key in JSON object (javascript)
(7 answers)
Closed 2 years ago.
I have a varible, if I print it out, I see this output:
Well, if I'd know the number '118', it would be easy, but in the program where I am using it, I don't know it. So is there any mode to get it without knowing that value?

You could use the Object.keys function to retrieve all keys of your object.
The Object.keys returns an array, you can then access the first element of that array like any other array.
const JSONString = '{"118": {"input1": 6, "input2": 1, "input3": 3}}';
const json = JSON.parse(JSONString);
const keys = Object.keys(json);
console.log(keys[0]);

Related

Parsing and reformatting an object to a simple javascript array [duplicate]

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.
Suppose I have an object from that has a key and value pair and I want to reformat it into a simple javascript array:
var obj = {STRUCTURE_: '004A006', SEG_ID: '04081591__,0.12,0.25,65850'}
that looks like this:
['004A006', '04081591__,0.12,0.25,65850']
Whats the easiest way to do that?
Use Object.values to get the object's enumerable property values:
var obj = {STRUCTURE_: '004A006', SEG_ID: '04081591__,0.12,0.25,65850'}
const res = Object.values(obj)
console.log(res)

How to create an object with specific key and value from an array in JavaScript? [duplicate]

This question already has answers here:
How to create an object from an Array of key-value pairs?
(7 answers)
Closed 2 years ago.
I would like to create a JS object made of specific array values. For example, I have an array ["bananas", 5] and I would like to convert it into an object {"bananas" : 5}. Could you please let me know how could I do that?
Object.fromEntries can be used to create an object from an array of arrays:
let input = ["bananas", 5];
let output = Object.fromEntries([input]);
console.log(output);
Where each two-elements array represents a key-value pair from your new object.

Setting JSON key with indexed array value [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I am trying to populate a JSON object by automatically setting the keys for an JSON object based on the string value of another array. For example,
var test = ["a","b"]
{test[0]:"A"}
However, I get a Syntax error when I do this, if I manually set the value as the string as shown in the third line {"a":"A"} this issue does not happen. I've checked that test[0] does indeed print out "a" and its datatype is a string. Is there any reason why this might be happening?
Try the following:
var test = ["a","b"]
var obj = {
[test[0]]:"A"
};
console.log(obj);

How to access json object from array [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
In my code one of the promise returning the result like this
[{"success":true,"fileName":"./tmp/test.txt"}]
and I want to convert it into Json. how to convert it?
output:-{"success":true,"fileName":"./tmp/test.txt"}
You receive an array containing one object.
Just access array's first element, like :
var array = [{"success":true,"fileName":"./tmp/test.txt"}];
var fileName = array[0].fileName;

Dynamically assign name to object array [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 10 years ago.
I have a javascript issue.
If I have an object array objAr, the object consists of id,name.
If I was to access objAr[0].id it returns the id value of the first object. What would happen if the object is dynamic and therefore I do not know what it consists of, is there a way to dynamically call the Object attribute?
Currently I am creating another array
var theArr = new Array("id", "name");
and call:
objAr[0].theArr[0] instead of objAr[0].id.
Is there a way to do this better using Javascript?
With Javascript you can call all of the attributes in an object without knowing the keys.
See below:
for(key in objAr[0]) {
console.log(objAr[0][key]);
}
If you just wanted the first attribute you could run:
for(key in objAr[0]) {
var attFirst = objAr[0][key];
break;
}
Additionally for the JS array you could have used square brackets.
var theArr = ["id", "name"];
hope that helps
In javascript you can always use the "array notation" in place of the "dot notation"
So these 2 lines are the same
objAr[0].id
objAr[0]["id"]

Categories

Resources