I want to access an element in an array in javascript without having to do a for loop to access it.
Here's my array:
var array = [{
"title": "Warnings",
"numbers": 30,
"content": [{
"number": 3001,
"description": "There may be a problem with the device you are using if you use the default profile"
}]
}, {
"title": "Errors",
"numbers": 20,
"content": [{
"number": 1000,
"description": "No network is loaded"
}]
}]
I want to access the "content" attribute of "Warnings" without doing a for loop.What I'm currently doing to access it is the following:
var content;
for(a in array) {
if(a.title == "Warnings") {
content = a.content;
break;
}
}
Is that something feasible in javascript ?
-
5unless you know the specific index of the object you wish to access you are going to have to loop in some manner. Also it is best to use the for(var i = ...) for loop rather than for-in loop. For-in is meant for objects :developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…scrappedcola– scrappedcola2013年06月25日 18:34:29 +00:00Commented Jun 25, 2013 at 18:34
-
I'm just wondering if there's a built in function or something else in javascript that does thisEtienne Noël– Etienne Noël2013年06月25日 18:35:20 +00:00Commented Jun 25, 2013 at 18:35
-
not on objects and not cross browser afaikscrappedcola– scrappedcola2013年06月25日 18:36:40 +00:00Commented Jun 25, 2013 at 18:36
-
2If there is a function that does this, it will still be looping internally.Andrew Clark– Andrew Clark2013年06月25日 18:38:09 +00:00Commented Jun 25, 2013 at 18:38
-
if you are using jquery you might look at grep:api.jquery.com/jQuery.grep but as F.J. states it will just be wrapping the for loop in another function and might not be necessarily as efficient as what you already have.scrappedcola– scrappedcola2013年06月25日 18:38:10 +00:00Commented Jun 25, 2013 at 18:38
4 Answers 4
Probably the best way to handle this is to change your data to an object instead, and access the object elements by the "title":
var data = {
"Warnings": {
"numbers": 30,
"content": [
{
"number" : 3001,
"description" : "There may be a problem with the device you are using if you use the default profile"
}
]
},
"Errors": {
"numbers": 20,
"content": [
{
"number": 1000,
"description": "No network is loaded"
}
]
}
};
Then, you can access the warnings as data.Warnings and the errors as data.Errors.
You can test that they exist with if (data.Warnings) or if (data.Errors) if you don't mind a null failing the test, or if (data.Warnings === undefined) if you'd rather test if the data exists at all.
Using this updated format, to access the content of warnings similar to what you have with '' returned if the data is not available, you would use something like:
var content = data.Warnings ? data.Warnings.content : '';
2 Comments
var content = ar.filter(function(v) {
return v.title == 'Warnings';
})[0].content;
Comments
If you know the index you can simply access it like this:
array[0].content
Comments
Try using http://jsonselect.org/
you can do what you want using selector:
.title:val("Warnings") ~ .content