Javascript string into array depth object [duplicate] - javascript

This question already has answers here:
Convert a JavaScript string in dot notation into an object reference
(34 answers)
Closed 4 years ago.
Sorry if the title is confusing, I'm confused my self.
So its like this, I get a string from api like this: 'data.data.type'.
And I need to turn it into this response['data]['data]['type'].
Any idea how I can achieve this?

Assuming response is a single object , you can get the value of type like this
var response = {
data: {
data: {
type: "Here is type"
}
}
}
console.log(response['data']['data']['type'])

Related

How to extract "fields" from JavaScript Object [duplicate]

This question already has answers here:
How to list the properties of a JavaScript object?
(18 answers)
Closed 4 years ago.
This is my spinet:
$scope.foodtypes = {
'GRAINS': {
'HOT': ['Rice','Beans','Sogum'],
'ROOT': ['Yam','Potato','Cassava']
}
}
I like to store the exact word "GRAINS" or "ROOT" in a variable for decision making:
something like:
if foodtypes is "GRAINS"
then ......
if foottypes is "ROOT"
then ......
Please how do I capture "GRAIN" or "ROOT" for use?
Assuming that foodtypes is a JSON string that represents an object that has all food types as first-level key, you could do something like this:
Object.keys(JSON.parse($scope.foodtypes));
This will return an array of strings ["GRAINS", "ROOT"].

Access value in array object [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 4 years ago.
I have this object:
var myValues = {
55bdf7bda89de40349854077: ["hello"]
55be0c77a89de403498540bc: ["goodbye"]
55be0e22a89de403498540c1: ["hey there!"]
}
And a variable as contains an id:
var id = '55be0e22a89de403498540c1';
I want to find in the object by this id and get the value in array.
I try to find with:
myValues.id[0]
but ... not works ;/
Anybody can help me?
You need to do myValues[id] or myValues['55be0e22a89de403498540c1']. It's a json object, not a primitive array so you can't access it with indexes like you are trying to do.
Your array should like,
var myValues = {
'55bdf7bda89de40349854077': "hello",
'55be0c77a89de403498540bc': "goodbye",
'55be0e22a89de403498540c1': "hey there!"
}
alert(myValues["55be0e22a89de403498540c1"])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
May this will help you

getJSON replace object with variable [duplicate]

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);

Add a new attribute to json object [duplicate]

This question already has answers here:
Add new attribute (element) to JSON object using JavaScript
(11 answers)
Add property to each object in the array [duplicate]
(2 answers)
Closed 6 years ago.
I have a JSON object of the following format
[{"field1":1,"field2":"A","field3":"B"},
{"field1":2,"field2":"B","field3":"C"},
{......},
{......},
]
I need to add a new attribute to each row based on some calculations.
Expected result
[{"field1":1,"field2":"A","field3":"B","field4"="generatedVal1"},
{"field1":2,"field2":"B","field3":"C","field4"="generatedVal2"},
{......},
{......},
]
How can I achieve this using javascript?
Use Array.prototype.forEach method:
[
{"field1":1,"field2":"A","field3":"B"},
{"field1":2,"field2":"B","field3":"C"}
]
.forEach(obj => {
obj.field4 = 'Something'
})
Sidenote on terminology: you don't have any JSON, you have javascript array (object). JSON is a string representation of this object, but in your question you are talking about object itself.

How to convert string to object in Angularjs [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 6 years ago.
I have a string like :
$scope.text = '"{\"firstName\":\"John\",\"age\":454 }"';
and I want to convert to js object:
$scope.tmp = {"firstName":"John","age":454 };
Note: JSON.parse() doesn't work!!
It's my sample in codepen
You can do it with angular.fromJson()
in your sample, it would have been $scope.tmp = angular.fromJson($scope.text);
The difference between JSON.Parse() and angular.fromJson, is that angular will check to make sure a string is provided. If it is already an object, it will return the same object.

Categories