This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 1 year ago.
I have an object of objects in which I want to print all the name values, how can I do this easily without having to hardcode each one?
const myObj = {
results: {
one: {
name: "o",
value: 1
}
two: {
name: "t",
value: 2
}
}
ignore: "this"
}
I can make it work with myObj.results.one.name, but I'd rather be able to do something like a loop where I can swap out the middle value like results.[1].name preventing the hard coding of 20 values.
Is there a simple way to do this without hardcoding each one. (The end use is to add them all to an array and use that to populate a table to show name and values)
This linked answer doesn't answer my question as this is an array of objects so solutions fail, and the second isn't nested
Related
This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 3 years ago.
My object is already defined.
I want to insert variables at particular indexes in the object.
Given message object:
message=[{
person:{
actions:[{test:1},{test:2}]
},
person:{}
},
{
person:{
actions:[{test:3},{test:4}]
},
person:{}
}]
1) If my string = "message[0].person.actions[1].visible".
This means I need to insert visible=null at index=1 of actions.
So message object becomes:
message=[{
person:{
actions:[{test:1},{test:2,visible:null}]
}
},
{
person:{
actions:[{test:3},{test:4}]
}
}]
2) If string="message[1].person.accessible",
So message Object become:
message=[{
person:{
actions:[{test:1},{test:2}]
}
},
{
person:{
accessible:null,
actions:[{test:3},{test:4}]
}
}]
I thought of doing split('[') and get that index and insert in that object.
Let me know if there are any libraries out there simplifying this for more complex data structure.
This question got closed along with a link to question [Accessing nested JavaScript objects with string key
My question is different than above mentioned link. I am asking about inserting a new field at particular location in object.
Just mutate the object by assigning it a property.
message[0].person.actions[1].visible = null;
This should work.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I am trying to read from a JSON array with a nested array which has its value name spaced out. So I get an error whenever I run the code.
var error = [
{
"LessonName":"Understanding Multiplication",
"LessonID":"13343",
"no of questions":[{"Locked":"31","Unlocked":5}]
},
{
"LessonName":"Finding Unknown Values ",
"LessonID":"13424",
"no of questions":[{"Locked":"34","Unlocked":5}]
}
]
function jsd(){
document.write(error[0].LessonName);
document.write(error[0].'no of questions'[0].Locked);
}
document.write(error[0]."no of questions"[0].Locked); Doesn't seem to display.
You may use a property accessor with brackets for the string.
error[0]['no of questions'][0].Locked
You must use this syntax for strings with spaces.
document.write(error[0]['no of questions'][0].Locked);
This question already has answers here:
How do I enumerate the properties of a JavaScript object? [duplicate]
(14 answers)
Closed 6 years ago.
I have a JSON-Array and want to loop though it. I know that there is no foreach-loop (like in PHP), but is there a possibility to access the field index of the array?
This is my json:
{
'username': 'Karl',
'email': 'xyz#abc.xx',
'user_id': 5,
...
}
I have some span elements on my page with a data-field attribute. E.g:
<span data-field="username">xyz</span>
The json-array is the return of an ajax-request. With it, I want to replace every element where the data-field="" matches the array-index.
Right know, I do it this way for each element in the json-array:
$("[data-field='username']").text(data.username);
$("[data-field='email']").text(data.email);
But with more and more elements this becomes a little bit ugly^^
Any other possibilties? I read something about transforming the json to an object, but I have no idea how to do.
The for each loop in javascript would look like below.
for(var key in Json){
$("[data-field='"+ key +"']").text(Json[key]);
}
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
I'm new to JavaScript and I've been stuck on this for a little while now. Let's say I have an object inside an array inside an object, like so:
var myCrazyObject = { "name": "A ridiculous object", "some array": [7, 9, { purpose: "confusion", number: 123 }, 3.3], "random animal": "Banana Shark"};
Now I know that I can access the "some array" attribute thusly:
myCrazyObject["some array"]
So, the part that I'm stuck on is, how do I access the purpose or number attributes?
It is the third element in your array, so you can access it by index:
myCrazyObject["some array"][2].purpose
or if you prefer the equivalent:
myCrazyObject["some array"][2]["purpose"]
would return "confusion". Obviously it's pretty brittle stuff. Normally you should be storing elements of the same type inside an array and not some integers at the beginning and then another arbitrary object. If for some reason the third element in this array wasn't an object you would get an error. So it's up to you to make the proper error handling here or fix your data input in order to bring some consistency here.
This question already has answers here:
Merge JS objects without overwriting
(4 answers)
Closed 9 years ago.
I am working in jQueryMobile and PhoneGap.
I Have 2 objects: eup and gld. The length of eup is 22, and the length of gld is 6.
I have tried:
//common.push(eup,gld);
//common.join(eup,gld);
//common.concat(eup,gld);
alert(common.length) // 2
When I check common.length, it's 2.
But for my logic I need it as 1. That means merge eup and gld and shows its length as 1. I got the result of eup and gld from two different APIs and it's in JSON format.
The main thing is the tags of both objects are identical. So I think it should be possible to merge these values as one and show its length as ONE.
Is there any solution for this????
It sounds like you want to extend an object, not concatenate an array.
var common = $.extend({}, eup, gld);
However, this will overwrite values with common keys. To merge without overwriting, the solution is explained at Merge JS objects without overwriting.