0

I'm new to JQuery and have to search a json object with jquery. the json structure is as follows:

[
    ["12345","Mary Smith","789 Main Street","Orlando","FL","32808"],
    ["33333","James Richards","55 High St","Miami","WV","23412"]
]

I want to search the object on the basis of first value i.e. 12345 and return the whole object.

How can I do this?

2 Answers2

0

You can use simple javascript as:

var entry = myJson["12345"];

or for Jquery need to write a function (as doesn't work on plain object literals):

function getObjects(obj, key) {

var objects = [];
  for (var i in obj) {
    if (obj[i][0] == key) {  
        return obj;
    }        
  }
 return objects;
}

Use like so:

getObjects(myJson, '12345');

Here is working Demo

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0
var queryJson = [["12345","Mary Smith","789 Main Street","Orlando","FL","32808"],["33333","James Richards","55 High St","Miami","WV","23412"]];

alert(queryJson[0]);
// will return a string array

alert(queryJson[0][0]);
// will return a string value "12345"

alert(queryJson[1][0]);
// will return a string value "33333"

I think I helped you. bye