0

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...

4
  • can you give sample inputs Commented Jan 4, 2018 at 6:50
  • I need to fetch data from the second array Neither of first or second are an array. Commented Jan 4, 2018 at 6:51
  • 1
    first - {"0":"1","2":"1"}, second {"0":"a", "1":"x","2":"y"}, I need to get out put like this {"0":"a","2":"y"}, Commented Jan 4, 2018 at 6:51
  • 2
    Both of your sample inputs are objects and not arrays Commented Jan 4, 2018 at 6:53

3 Answers 3

2

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this is what I want
This gives an array of objects and not object with keys as the OP mentioned in his comment
1

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)

Comments

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":"[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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.