Vineesh K S
Vineesh K S

Reputation: 1964

Compare JSON object array using Jquery

I have a selected list object like this {"0":"1","2":"1"}, I want to compare it with another array like the following

{
  "0": {
    "id": 1,
    "salutation": "Dr.",
    "firstname": "Kapil",
    "lastname": "Dev",
    "gender": "Male ",
    "email": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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"
  }
}

on the basis of the keys, means that only 0 & 2 are selected and I need to fetch data from the second object having key 0 & 2, (excluding 1 ), how can I do this? I am new to this area...

Upvotes: 0

Views: 83

Answers (3)

Jonny Xiang
Jonny Xiang

Reputation: 11

<!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":"[email protected]","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":"[email protected]","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":"[email protected]","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>

Upvotes: 1

Shubham Khatri
Shubham Khatri

Reputation: 281656

Considering you only want to the object filtered out from your selection, you could use forEach on Object.keys of your selected object

var obj = {"0":"1","2":"1"};
var newobj = {
  "0": {
    "id": 1,
    "salutation": "Dr.",
    "firstname": "Kapil",
    "lastname": "Dev",
    "gender": "Male ",
    "email": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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)

Upvotes: 1

zabusa
zabusa

Reputation: 2719

var obj = {"0":"1","2":"1"};
var newobj = {
  "0": {
    "id": 1,
    "salutation": "Dr.",
    "firstname": "Kapil",
    "lastname": "Dev",
    "gender": "Male ",
    "email": "[email protected]",
    "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": "[email protected]",
    "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": "[email protected]",
    "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)

Upvotes: 2

Related Questions