如何从对象数组中查找指定的项目 [英] How to find an specified item from an object array

查看:86
本文介绍了如何从对象数组中查找指定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jsondata,它不过是一个对象数组.

如下图所示:

JSONdata-> [0]-[universitycode] = 10001,[cgpa] = 8.3,[duration] = 80hrs

[1]-[universitycode] = 10002,[cgpa] = 7.3,[duration] = 80hrs
[2]-[universitycode] = 10003,[cgpa] = 9.3,[duration] = 90hrs


现在,我必须找到具有大学代码10001的对象.

I am having one jsondata, which is nothing but an object array.

It looks like below:

JSONdata--> [0] - [universitycode]=10001,[cgpa]=8.3,[duration]=80hrs

[1] - [universitycode]=10002,[cgpa]=7.3,[duration]=80hrs
[2] - [universitycode]=10003,[cgpa]=9.3,[duration]=90hrs


Now I have to find which object has universitycode 10001.

Can you please provide the easiest way to find this?

推荐答案

看看这个示例

Take a look at this sample

/*
function findIndexByKeyValue: finds "key" key inside "ob" object that equals "value" value
example: findIndexByKeyValue(students, 'name', "Jim");
object: students = [
   {name: 'John', age: 100, profession: 'Programmer'},
   {name: 'Jim', age: 50, profession: 'Carpenter'}
];
would find the index of "Jim" and return 1
*/
function findIndexByKeyValue(obj, key, value)
{
    for (var i = 0; i < obj.length; i++) {
        if (obj[i][key] == value) {
            return i;
        }
    }
    return null;
}



希望这对进一步的查询有所帮助或传达信息.

如果有帮助,请 投票 接受答案 .



Hope this helps or communicate regarding further query.

Please vote and Accept Answer if it Helped.


这篇关于如何从对象数组中查找指定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆