使用Jquery比较JSON对象数组

时间:2018-01-04 06:48:13

标签: javascript arrays json

我有一个像{"0":"1","2":"1"},这样的选定列表对象 我想将它与另一个数组进行比较,如下所示

{
  "0": {
    "id": 1,
    "salutation": "Dr.",
    "firstname": "Kapil",
    "lastname": "Dev",
    "gender": "Male ",
    "email": "kapil.dev@aggenome.com",
    "phone": 1232423415,
    "usertype": "student",
    "institution": "AgriGenome Labs Pvt Ltd",
    "department": "Lab",
    "country": "India",
    "conferenceitem": "2017 NGBT Conference ",
    "conferenceitemid": "39",
    "amount": 2800,
    "actual_amount": "5000.00",
    "currency": "INR",
    "group": "Lead",
    "accompany": "No",
    "password": null,
    "mailsend": "Yes"
  },
  "1": {
    "id": 2,
    "salutation": "Mr.",
    "firstname": "Sunil",
    "lastname": "Gavaskar",
    "gender": "Male ",
    "email": "sunil.gavaskar@aggenome.com",
    "phone": 1232423415,
    "usertype": "commercial",
    "institution": "AgriGenome Labs Pvt Ltd",
    "department": "Bio Info",
    "country": "India",
    "conferenceitem": "2017 NGBT Conference ",
    "conferenceitemid": "31",
    "amount": "3100.00",
    "actual_amount": "10000.00",
    "currency": "INR",
    "group": "Yes",
    "accompany": "No",
    "password": null,
    "mailsend": "Yes"
  },
  "2": {
    "id": 3,
    "salutation": "Mr.",
    "firstname": "Anil",
    "lastname": "Kumble",
    "gender": "Male ",
    "email": "anil.kumble@aggenome.com",
    "phone": 1232423415,
    "usertype": "student",
    "institution": "AgriGenome Labs Pvt Ltd",
    "department": "Support",
    "country": "India",
    "conferenceitem": "Accompanying Person",
    "conferenceitemid": "5",
    "amount": 1900,
    "actual_amount": "5000.00",
    "currency": "INR",
    "group": "No",
    "accompany": "Yes",
    "password": null,
    "mailsend": "No"
  }
}

基于键,意味着只有0&选择2并且我需要从具有键0和0的第二对象获取数据。 2,(不包括1),我该怎么做?我是这个地区的新手......

3 个答案:

答案 0 :(得分:2)

var obj = {"0":"1","2":"1"};
var newobj = {
  "0": {
    "id": 1,
    "salutation": "Dr.",
    "firstname": "Kapil",
    "lastname": "Dev",
    "gender": "Male ",
    "email": "kapil.dev@aggenome.com",
    "phone": 1232423415,
    "usertype": "student",
    "institution": "AgriGenome Labs Pvt Ltd",
    "department": "Lab",
    "country": "India",
    "conferenceitem": "2017 NGBT Conference ",
    "conferenceitemid": "39",
    "amount": 2800,
    "actual_amount": "5000.00",
    "currency": "INR",
    "group": "Lead",
    "accompany": "No",
    "password": null,
    "mailsend": "Yes"
  },
  "1": {
    "id": 2,
    "salutation": "Mr.",
    "firstname": "Sunil",
    "lastname": "Gavaskar",
    "gender": "Male ",
    "email": "sunil.gavaskar@aggenome.com",
    "phone": 1232423415,
    "usertype": "commercial",
    "institution": "AgriGenome Labs Pvt Ltd",
    "department": "Bio Info",
    "country": "India",
    "conferenceitem": "2017 NGBT Conference ",
    "conferenceitemid": "31",
    "amount": "3100.00",
    "actual_amount": "10000.00",
    "currency": "INR",
    "group": "Yes",
    "accompany": "No",
    "password": null,
    "mailsend": "Yes"
  },
  "2": {
    "id": 3,
    "salutation": "Mr.",
    "firstname": "Anil",
    "lastname": "Kumble",
    "gender": "Male ",
    "email": "anil.kumble@aggenome.com",
    "phone": 1232423415,
    "usertype": "student",
    "institution": "AgriGenome Labs Pvt Ltd",
    "department": "Support",
    "country": "India",
    "conferenceitem": "Accompanying Person",
    "conferenceitemid": "5",
    "amount": 1900,
    "actual_amount": "5000.00",
    "currency": "INR",
    "group": "No",
    "accompany": "Yes",
    "password": null,
    "mailsend": "No"
  }
}

var newArray = Object.keys(obj).map(item => {
return newobj[item]})
console.log(newArray)

答案 1 :(得分:2)

考虑到您只想要从您的选择中过滤掉的对象,您可以在所选对象的Object.keys上使用forEach



var obj = {"0":"1","2":"1"};
var newobj = {
  "0": {
    "id": 1,
    "salutation": "Dr.",
    "firstname": "Kapil",
    "lastname": "Dev",
    "gender": "Male ",
    "email": "kapil.dev@aggenome.com",
    "phone": 1232423415,
    "usertype": "student",
    "institution": "AgriGenome Labs Pvt Ltd",
    "department": "Lab",
    "country": "India",
    "conferenceitem": "2017 NGBT Conference ",
    "conferenceitemid": "39",
    "amount": 2800,
    "actual_amount": "5000.00",
    "currency": "INR",
    "group": "Lead",
    "accompany": "No",
    "password": null,
    "mailsend": "Yes"
  },
  "1": {
    "id": 2,
    "salutation": "Mr.",
    "firstname": "Sunil",
    "lastname": "Gavaskar",
    "gender": "Male ",
    "email": "sunil.gavaskar@aggenome.com",
    "phone": 1232423415,
    "usertype": "commercial",
    "institution": "AgriGenome Labs Pvt Ltd",
    "department": "Bio Info",
    "country": "India",
    "conferenceitem": "2017 NGBT Conference ",
    "conferenceitemid": "31",
    "amount": "3100.00",
    "actual_amount": "10000.00",
    "currency": "INR",
    "group": "Yes",
    "accompany": "No",
    "password": null,
    "mailsend": "Yes"
  },
  "2": {
    "id": 3,
    "salutation": "Mr.",
    "firstname": "Anil",
    "lastname": "Kumble",
    "gender": "Male ",
    "email": "anil.kumble@aggenome.com",
    "phone": 1232423415,
    "usertype": "student",
    "institution": "AgriGenome Labs Pvt Ltd",
    "department": "Support",
    "country": "India",
    "conferenceitem": "Accompanying Person",
    "conferenceitemid": "5",
    "amount": 1900,
    "actual_amount": "5000.00",
    "currency": "INR",
    "group": "No",
    "accompany": "Yes",
    "password": null,
    "mailsend": "No"
  }
}

const result = {}
Object.keys(obj).forEach(key => {
    result[key] = newobj[key]
})
console.log(result)




答案 2 :(得分:1)

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>

</body>
<script type="text/javascript">
    var flag = {"0":"1","2":"1"};

    var data = {"0":{"id":1,"salutation":"Dr.","firstname":"Kapil","lastname":"Dev","gender":"Male ","email":"kapil.dev@aggenome.com","phone":1232423415,"usertype":"student","institution":"AgriGenome Labs Pvt Ltd","department":"Lab","country":"India","conferenceitem":"2017 NGBT Conference ","conferenceitemid":"39","amount":2800,"actual_amount":"5000.00","currency":"INR","group":"Lead","accompany":"No","password":null,"mailsend":"Yes"},"1":{"id":2,"salutation":"Mr.","firstname":"Sunil","lastname":"Gavaskar","gender":"Male ","email":"sunil.gavaskar@aggenome.com","phone":1232423415,"usertype":"commercial","institution":"AgriGenome Labs Pvt Ltd","department":"Bio Info","country":"India","conferenceitem":"2017 NGBT Conference ","conferenceitemid":"31","amount":"3100.00","actual_amount":"10000.00","currency":"INR","group":"Yes","accompany":"No","password":null,"mailsend":"Yes"},"2":{"id":3,"salutation":"Mr.","firstname":"Anil","lastname":"Kumble","gender":"Male ","email":"anil.kumble@aggenome.com","phone":1232423415,"usertype":"student","institution":"AgriGenome Labs Pvt Ltd","department":"Support","country":"India","conferenceitem":"Accompanying Person","conferenceitemid":"5","amount":1900,"actual_amount":"5000.00","currency":"INR","group":"No","accompany":"Yes","password":null,"mailsend":"No"}};

    //loop the flag
    $.each( flag, function(i,c){

        //loop the data
        $.each(data,function(di,dc){
            if(i == di)
            {
                //data you want
                console.log(dc)
            }
        });        

    });

</script>
</html>