I have a JSON response like this:
var errorLog = "[[\"p\",\"Please add pany name!\"],
[\"zip\",\"Please add zip code!\"],
...
Which I'm deserializing like this:
var log = jQuery.parseJSON(errorLog);
Now I can access elements like this:
log[1][1] > "Please add pany name"
Question:
If I have the first value p, is there a way to directly get the 2nd value by doing:
log[p][1]
without looping through the whole array.
Thanks for help!
I have a JSON response like this:
var errorLog = "[[\"p\",\"Please add pany name!\"],
[\"zip\",\"Please add zip code!\"],
...
Which I'm deserializing like this:
var log = jQuery.parseJSON(errorLog);
Now I can access elements like this:
log[1][1] > "Please add pany name"
Question:
If I have the first value p, is there a way to directly get the 2nd value by doing:
log[p][1]
without looping through the whole array.
Thanks for help!
Share Improve this question edited Jun 8, 2012 at 17:41 Afshin Mehrabani 35k33 gold badges144 silver badges209 bronze badges asked Jun 8, 2012 at 16:42 frequentfrequent 28.6k61 gold badges187 silver badges336 bronze badges 7- I'm getting undefined. I tried with toLowerCase() and without – frequent Commented Jun 8, 2012 at 16:44
- Can you change the array to an object? – SLaks Commented Jun 8, 2012 at 16:44
- @SLaks: no, this is bound into to many things client and server-side – frequent Commented Jun 8, 2012 at 16:45
-
Does
p
hold the value of the first-dimension array or the index within that array? – Matt Commented Jun 8, 2012 at 16:46 - 1 You would have to search for p and then get the second value. Currently you are using it as an index and so should be an int. you could use a for loop until you found p and then access the second element from there – GreenGiant Commented Jun 8, 2012 at 16:47
4 Answers
Reset to default 3No. Unless the 'value' of the first array (maybe I should say, the first dimension, or the first row), is also it's key. That is, unless it is something like this:
log = {
'p': 'Please add a pany name'
.
.
.
}
Now, log['p']
or log.p
is legal.
There are two was to do this, but neither avoids a loop. The first is to loop through the array each time you access the items:
var val = '';
for (var i = 0; i < errorLog.length; i++) {
if (errorLog[i][0] === "p") {
val = errorLog[i][1];
break;
}
}
The other would be to work your array into an object and access it with object notation.
var errors = {};
for (var i = 0; i < errorLog.length; i++) {
errors[errorLog[i][0]] = errorLog[i][1];
}
You could then access the relevant value with errors.p
.
If you're only looking once, the first option is probably better. If you may look more than once, it's probably best to use the second system since (a) you only need to do the loop once, which is more efficient, (b) you don't repeat yourself with the looping code, (c) it's immediately obvious what you're trying to do.
No matter what you are going to loop through the array somehow even it is obscured for you a bit by tools like jQuery.
You could create an object from the array as has been suggested like this:
var objLookup = function(arr, search) {
var o = {}, i, l, first, second;
for (i=0, l=arr.length; i<l; i++) {
first = arr[i][0]; // These variables are for convenience and readability.
second = arr[i][1]; // The function could be rewritten without them.
o[first] = second;
}
return o[search];
}
But the faster solution would be to just loop through the array and return the value as soon as it is found:
var indexLookup = function(arr, search){
var index = -1, i, l;
for (i = 0, l = arr.length; i<l; i++) {
if (arr[i][0] === search) return arr[i][1];
}
return undefined;
}
You could then just use these functions like this in your code so that you don't have to have the looping in the middle of all your code:
var log = [
["p","Please add pany name!"],
["zip","Please add zip code!"]
];
objLookup(log, "zip"); // Please add zip code!
indexLookup(log, "p"); // Please add pany name!
Here is a jsfiddle that shows these in use.
Have you looked at jQuery's grep
or inArray
method?
See this discussion
Are there any jquery features to query multi-dimensional arrays in a similar fashion to the DOM?