How to access this attribute [duplicate] - javascript

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.

Related

How to easily print nested objects without naming each one [duplicate]

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

JS How do I output an array inside an array in the terminal? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
How do I output a certain value which is inside of an array inside of another array?
If this doesn't make sense let me demonstrate this
let sports = [
['rowing', 'gymnastics'],
['martial arts', 'tennis'],
['soccer', 'basketball']
]
So what I did was sort sports into categories: individual sports, dual, team.
I just simply want to output in the terminal only one of these sports.
Therefore what I tried was
console.log(sports[1[1]])
In this way I thought that it would select the second array inside of sports (dual sports) and then pick the second item inside of that array (tennis).
This resulted in undefined, why is this? and in what way do I output tennis to the terminal then'
You should provide request like this:
console.log(sports[1][1])

How to update a javascript/json object based on a string defining path of field to be inserted to object [duplicate]

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.

How to access Json [duplicate]

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

JavaScript for(-each) loop [duplicate]

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

Categories

Resources