开发者

Looking inside an object for a specific string using JavaScript

On JavaScript, I have the followi开发者_开发知识库ng JSON:

var mJSON = {
    "monster":[
        {"id":"150","name":"Richard"},
        {"id":"100","name":"Gregory"},
        {"id":"200","name":"Rachel"},
        {"id":"250","name":"Mike"}
    ]
}

I need to refine this object by a string inputted by the user. For example: "100". The result should be a new JSON like this:

var zJSON = {
    "monster":[
        {"id":"100","name":"Gregory"}
    ]
}

I tried looking in Google on easy ways to run through a JavaScript object searching for a string, but without success. There's nothing like jQuery's $.inArray too, as far I know. Anyone has any idea? I'm thinking about converting this JSON into a string, grep it for the value inputted by the user, and then converting the string to JSON again, but I think this will be too troublesome for something that could be easy to achieve.


How about using $.map?

var id = 100;
var result = $.map(monsters, function(monster){
    return monster.id == id ? monster : null;
});

JQuery.map() applies function to each argument of the array (monsters) and produces the new array that contains the values returned by the function. What is important in this case is that if function returns null then the element is removed from the resulting array.

EDIT: As @Jan has kindly suggested in his comment $.grep suits even better! Here is the code example for your monsters:

var id = 100;
var result = $.grep(monsters, function(monster){
    return monster.id == id;
});


Why don't you just loop through the array removing stuff that doesn't match?


Without using libraries, you could do something like this:

var mJSON = {
    "monster":[
        {"id":"150","name":"Richard"},
        {"id":"100","name":"Gregory"},
        {"id":"200","name":"Rachel"},
        {"id":"250","name":"Mike"}
    ]
};

var searchTerm = "100";

var result = mJSON.monster.filter(function(e){
    // if you want loose(r) searches, you could use a regex here
    // rather than explicit equality
    if(e.id == searchTerm)
    {
        return true;
    }
});
console.log(result);

http://jsfiddle.net/dbrecht/MZQzM/


Use the grep method. Example:

var obj = {
  monster: [
    { id: "150", name: "Richard" },
    { id: "100", name: "Gregory" },
    { id: "200", name: "Rachel" },
    { id: "250", name: "Mike" }
  ]
};

var input = "100";

var filtered = {
  monster: $.grep(obj.monster, function(e){
    return e.id == input;
  })
};


var mJSON = {
    "monster":[
        {"id":"150","name":"Richard"},
        {"id":"100","name":"Gregory"},
        {"id":"200","name":"Rachel"},
        {"id":"250","name":"Mike"}
    ]
};
var searhKey = "100";

var found = false, i = 0, pos = -1, l = MJSON.monster.length;
while(!found && i < l) {
    if(MJSON.monster[i].id == searchKey) {
        pos = i;
        found = true;
    } 
    i += 1;
}
if(found) {  
    alert(pos);
} else {
    alert("not found");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜