getJSON replace object with variable [duplicate] - javascript

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
I have json file called list.json:
{
"nvg":{
"Title":"title",
"Description":"description",
"Image":"",
"URL":{
"DEV":"https://dev.com",
"TEST":"https://test.com",
"PROD":"https://prod.com"
}
}
I have an array
var apps = ["nvg"];
I want to access the json data and extract the object based on the variable's value. Does anyone know how to pass the [i] into the json call?
var getConfig = $.getJSON( data, function( json ) {
var apps = ["nvg"];
apps.forEach(function(i){
alert(i);
alert(json.i.URL.DEV);
});
});
Like the code above, but to actually the "i" to pass the value defined in the array?

You should be able to access the data using your Javascript object like an object or array. This means you can do the following:
alert(json[i].URL.DEV);

Related

When using a variable to call an object from a JSON file, it doesn't call it at all [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 1 year ago.
Using JS in conjunction with a JSON file to turn a room ID into the room information.
function rooms(roomID) {
const roomsDB = require("./rooms.json")
let roomsID = "r".concat("", roomID).toString()
console.log(roomsDB.roomsID)
}
rooms(46)
This is the function I'm using to turn the ID into the info, and the rooms.json file is as follows:
{
"r46":"house"
}
Ideally, this would log house into the console, but I only get undefined.
Why isn't the JSON object being called properly?
To dynamically access an object property by name in a string variable you need to use brackets:
roomsDB[roomsID]
The dot (roomsDB.roomsID) is just like roomsDB["roomsID"], it doesn't substitute the variable.

Unable to get object data when used multiple-argument function [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I am unable to get the data of an object
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a.val[0])
}
fun('ab','ef')
It should output 'cd' but it is giving out error in the console
any idea how do i fix this...
Use bracket notation like so:
var a = {
'ab':'cd',
'ef':'gh',
'ij':'kl'
}
function fun(...val){
console.log(a[val[0]])
}
fun('ab','ef')
Your code was trying to get the property named val in a (doesn't exist), then get the first character/item of that value (trying to do this to undefined causes the error).

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

How to get key values from "array string" in javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Extract values from json string javascript
(3 answers)
Closed 6 years ago.
I have an array in Laravel controller like:
$succeeded[] = array('name' => $name,
'file' => $file
);
...
return $succeeded;
I use an ajax call for getting $succeeded, and I'll have a return string in js function composed of:
"[{"name":"sor.jpg","file":"399393.jpg"}]"
My question is how can I get "name" and "file" values from the string above?
Note: I didn't use any JSON object in controller, but returned just the array. However I wonder it is advantageous to use JSON object.
You first need to parse the response text into a JSON array using the native JSON.parse, and then you can extract the values from the object in the parsed array using dot notation.
var respText = "[{\"name\":\"sor.jpg\",\"file\":\"399393.jpg\"}]";
var arr = JSON.parse(respText)[0]; // Careful - this will throw on invalid JSON
var name = arr.name; // "sor.jpg"
var file = arr.file; // "399393.jpg"

How to get the value dynamically from JSON object [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 7 years ago.
I want to retrieve a JSON value dynamically from JSON object. Below is the code i am using to get the value from JSON object.
var jsonObj = JSON.parse(jsonData);
console.log(jsonObj);
jsonSplit = jsonToFind.split(htmlSplit+".")[1].trim();
console.log(jsonObj+"."+jsonSplit);
But I am getting [object Object].ensighten_tag.
Here ensighten_tag is the dynamic key value.
Can anyone suggest me how to get the value dynamically ?
console.log(JsonObj[jsonSplit])

Categories