How to construct object path dynamically [duplicate] - javascript

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 4 years ago.
I have a JSON object and I am iterating through it. I am using different values from different levels of it.
But I am not able to create path dynamically to reiterate the object.
var data= {
"algoName": "textClassification",
"hyperParams": {
"mode": {
"data_type": "string",
"default_value": "supervised",
"required": true,
"description": "The training mode",
"allowedValues": [
"supervised",
"unsupervised"
]
}
}
}
var key1;
for(var key in data.hyperParams) {
key1=key;
}
var text1 = "data.hyperParams" + key1
for(var key in text1) {
console.log(text1[key]);
}

Thank you T.J. Crowder. I modified code as following and it is working now.
for(var key in data.hyperParams[key1]) {
console.log(data.hyperParams[key1][key]);
}

Related

Parse Steam library folders file [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Find object by id in an array of JavaScript objects
(36 answers)
Converting JavaScript object with numeric keys into array
(17 answers)
Closed 17 days ago.
I've converted the file libraryfolders.vdf into json using https://github.com/p0358/vdf-parser
Now i'm stuck with something like this:
"libraryfolders": {
"0": {
"path": "D:\\\\Program Files (x86)\\\\Steam",
"label": "",
"contentid": -3675068714072690700,
"totalsize": 0,
"update_clean_bytes_tally": 156136761200,
"time_last_update_corruption": 0,
"apps": {
"357720": 1119505844,
"367450": 222650546,
"391540": 163095879,
}
},
"1": {
"path": "C:\\\\SteamLibrary",
"label": "",
"contentid": 8761260447083160000,
"totalsize": 126696288256,
"update_clean_bytes_tally": 0,
"time_last_update_corruption": 0,
"apps": {
"433340": 1230903210,
}
}
}
I need to loop through all the libraries and get path for gameID.
This is my code so far.
for (let i = 0; i < 999; i++) {
let lib = json.libraryfolders[i];
if (!lib) break;
let path = lib.path;
if (lib.apps.hasOwnProperty('433340')) {
console.log("app found in " + path)
}
}
And my question is: how can I make it more effective? How can I get the number of libs when it's not in array like brackets []?

Convert a string to a nested JavaScript object or JSON [duplicate]

This question already has answers here:
Javascript nested objects from string
(5 answers)
Closed 1 year ago.
I have the following string:
let str = "modules.mas.mas-helper-provider.assets.locales";
and would like to convert it to a nested JavaScript object (JSON), something like this result:
{
"modules": {
"mas": {
"mas-helper-provider": {
"assets": {
"locales": ""
}
}
}
}
}
You can split the string to an array, then reduceRight to create an object by reading each key.
let str = "modules.mas.mas-helper-provider.assets.locales";
var newObject = str.split(".").reduceRight((obj, next) => ({
[next]: obj
}), "");
console.log(newObject);

Get information from a JavaScript object that has a number as an index [duplicate]

This question already has answers here:
Unable to access JSON property with "-" dash [duplicate]
(5 answers)
Closed 6 years ago.
How can I retrieve information from a JavaScript object that has a numeric value as an index?
"element_count": 69,
"near_earth_objects": {
"2016-10-29": [ ... ]
I need to access the data inside that "2016-10-29" Array.
I have no problem accessing the other elements like this:
$.getJSON(Call, function(data1){
console.log(data1.element_count);
});
Like this to mix notation and retrieve sub-array element in a property of your Json object :
var data = {
"element_count": 69,
"near_earth_objects": {
"2016-10-29": ["subelement1", "subelement2", "subelement3"],
"2016-10-30": ["subelement11", "subelement12", "subelement13"]
}
};
console.log(data.near_earth_objects["2016-10-29"][1]);
x = {
"near_earth_objects" :
{
"2016-10-29" : 1
}
}
console.log(x["near_earth_objects"]["2016-10-29"])

How to get the least element from the values of a key in JSON? [duplicate]

This question already has answers here:
Obtain smallest value from array in Javascript?
(18 answers)
Closed 7 years ago.
Suppose i have following Json object.
{
"orderId": ["80055517"],
"orderItemId": [
"850057658",
"850057657",
"850057656"
]
}
Now i want the least value i.e 850057656 from orderitemId.
Assuming you do not have orderItemId sorted. You can get the result using following.
var obj = {
"orderId": ["80055517"],
"orderItemId": [
"850057658",
"850057657",
"850057656"
]
};
console.log(obj.orderItemId.sort()[0]);
var a = {
"orderId": ["80055517"],
"orderItemId": [
"850057658",
"850057657",
"850057656"
]
};
alert(a.orderItemId[a.orderItemId.length-1]);

Form array of property names found in a JavaScript Object [duplicate]

This question already has answers here:
Get array of object's keys
(8 answers)
Closed 7 years ago.
I have the following object
var columns = {ContributionType: "Employer Contribution",
Employee1: "0",
Employee2: "0",
Employee3: "0"
};
From this I need to form an array with they property keys alone like following
var keys=["ContributionType", "Employee1", "Employee2", "Employee3"];
The number of properties is dynamic
Question:
How can I achieve this using lodash or pure JavaScript?
Object.keys()
var columns = {ContributionType: "Employer Contribution",
Employee1: "0",
Employee2: "0",
Employee3: "0"
};
var keys = Object.keys(columns);
console.log(keys);
var arr=[];
for (var key in columns)
{
//by using hasOwnProperty(key) we make sure that keys of
//the prototype are not included if any
if(columns.hasOwnProperty(key))
{
arr.push(key);
}
}

Categories

Resources