最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the simplest way to search in JSON by attribute? - Stack Overflow

programmeradmin0浏览0评论
"names": [
    {
        "id": 17,
        "user_id": 9,
        "code": "de",
        "name": "Ich bin Hans",
        "created_at": "2017-07-31 12:43:19",
        "updated_at": "2017-07-31 12:43:19"
    },
    {
        "id": 18,
        "user_id": 9,
        "code": "en",
        "name": "My name is Hans",
        "created_at": "2017-07-31 12:43:19",
        "updated_at": "2017-07-31 12:43:19"
    }
]

Is there any method to get JSON object with code='en' from the above JSON array in jQuery?

I could do it with forloop but I thought maybe there is a simpler way to do it.

"names": [
    {
        "id": 17,
        "user_id": 9,
        "code": "de",
        "name": "Ich bin Hans",
        "created_at": "2017-07-31 12:43:19",
        "updated_at": "2017-07-31 12:43:19"
    },
    {
        "id": 18,
        "user_id": 9,
        "code": "en",
        "name": "My name is Hans",
        "created_at": "2017-07-31 12:43:19",
        "updated_at": "2017-07-31 12:43:19"
    }
]

Is there any method to get JSON object with code='en' from the above JSON array in jQuery?

I could do it with forloop but I thought maybe there is a simpler way to do it.

Share Improve this question edited Feb 24, 2019 at 15:44 ceejayoz 180k41 gold badges309 silver badges380 bronze badges asked Aug 4, 2017 at 8:37 NevermoreNevermore 1,7437 gold badges33 silver badges60 bronze badges 1
  • 1 Possible duplicate of ES6: Find an object in an array by one of its properties – Mohideen bin Mohammed Commented Aug 4, 2017 at 8:48
Add a comment  | 

3 Answers 3

Reset to default 12

Use Array.prototype.filter function in vanilla JS to filter out the object - see demo below:

var obj={names:[{id:17,user_id:9,code:"de",name:"Ich bin Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"},{id:18,user_id:9,code:"en",name:"My name is Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"}]};

var result = obj.names.filter(function(e){return e.code == 'en'})
console.log(result);

ES6 version is even simpler:

var obj={names:[{id:17,user_id:9,code:"de",name:"Ich bin Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"},{id:18,user_id:9,code:"en",name:"My name is Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"}]};

var result = obj.names.filter(e => e.code == 'en');
console.log(result);

Use Jquery grep function. Actually you are not searching object, you are searching though an array of objects (names).

Finds the elements of an array which satisfy a filter function. The original array is not affected.

 var input = {names:[{id:17,user_id:9,code:"de",name:"Ich bin Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"},{id:18,user_id:9,code:"en",name:"My name is Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"}]};
 
var result = $.grep(input.names, function(obj) {
    return obj.code === "en";
});
console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Another is Array#find function

var input = {names:[{id:17,user_id:9,code:"de",name:"Ich bin Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"},{id:18,user_id:9,code:"en",name:"My name is Hans",created_at:"2017-07-31 12:43:19",updated_at:"2017-07-31 12:43:19"}]};
 
var result = input.names.find(item => {
   return item.code == 'en'
})
console.log(result);

If you don't want to do it with a for loop, try the ES5 array function, filter

names = names.filer(function(item) {
  return item.code === 'en'
})
发布评论

评论列表(0)

  1. 暂无评论