JSObjects how to access to children dynamically [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 5 years ago.
I want to access a JSObject item dynamically:
jsonElement = {
name:"name",
subElement {
subElementName: "nameSubElement",
}
}
And I have the element as:
//level = "name"
jsonElement[level] -> it works
//level = "subElement.nameSubElement"
jsonElement[level] -> not correct. how to?
The important issue I can't do things like json.subElement.nameSubElement because I just have var level.

jsonElement.subElement.subElementName
or
jsonElement["subElement"]["subElementName"]

Related

cannot print component in array in javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 2 months ago.
i'm just a newbie on javascript , please help me this
var arr = [{"student-name":"Harry Potter","discipline":"Magic"}] ;
how can i get "Harry Potter" in this array , thanks for helping

How can I add an element to a json object in JavaScript? [duplicate]

This question already has answers here:
How to concatenate properties from multiple JavaScript objects
(14 answers)
Closed 6 months ago.
var obj={"student":[{"id":1,"name":"mark"}]}
For example I have this object I want to add another element to it such as this:
var teacher= "teacher":[{"id":1,"name":"Stacy"}]
to make this new object:
var objnew= {"student":[{"id":1,"name":"mark"}], "teacher":[{"id":1,"name":"Stacy"}] }
You can use this code.
var objnew = Object.assign({}, obj, teacher);

Referencing / Parsing an object [duplicate]

This question already has answers here:
How to iterate over a JavaScript object?
(19 answers)
How to loop through a plain JavaScript object with the objects as members
(28 answers)
How do I loop through or enumerate a JavaScript object?
(48 answers)
Iterate through object properties
(31 answers)
How to iterate (keys, values) in JavaScript? [duplicate]
(11 answers)
Closed 5 years ago.
Very simple question here, I have the following variable. set up in a format like so {item number: quantity, item number, quantity....}
var x = {4214061251:1,2497227651:2,4214140739:2};
My question is simply how do i loop through this properly to access each element.
I would like to be able to access the item numbers and quantities separately, how can I achieve this?
var x = {4214061251:1,2497227651:2,4214140739:2};
// Array of all keys 4214061251, 2497227651 ...
var keysArr = Object.keys(x);
console.log(keysArr);
// Iterate on keys
keysArr.forEach(
(el) => {
// To access keys
console.log("Key: "+el);
// To access values instead
console.log("Value: "+x[el]);
}
)

How to iterate array of objects [duplicate]

This question already has answers here:
Object.length undefined in javascript [duplicate]
(3 answers)
JavaScript object literal length === undefined?
(5 answers)
Closed 5 years ago.
Here is my jsfiddle
Here is my array looks like
var arr = [];
arr['alpha'] = {'a':'1','b':'1'};
arr['beta'] = {'a':'2','b':'4'};
console.log(arr)
When i take console.log(arr.length) It says 0. So i can't able to for loop this one
How can i iterate this array ?
Note :
I don't want to use jquery, i prefer only javascript

How to combine 2 array to an Object? [duplicate]

This question already has answers here:
Merge keys array and values array into an object in JavaScript
(14 answers)
Closed 8 years ago.
I have 2 arrays which are the same length like this:
$scope.access_name = ['group1','group2','group3','group4']
access_value=[1,2,1,3]
How can I combine to have result like this:
object_access = {'group1:1','group2:2','group3:1','group4:3'}
thankyou very much
var object_access={};
for(int i=0;i<access_value.length;i++)
{
object_access[$scope.access_name[i]]=access_value[i];
}

Categories