1

This is the main json list

allData=
{
    "searchList": [{
        "vaccinationId": 1,
        "vaccinationName": "vacc 1",
        "vaccinationNameAr": null
    }, {
        "vaccinationId": 2,
        "vaccinationName": "vacc 2",
        "vaccinationNameAr": null
    }, {
        "vaccinationId": 3,
        "vaccinationName": "vacc 3",
        "vaccinationNameAr": null
    }, {
        "vaccinationId": 4,
        "vaccinationName": "vacc 4",
        "vaccinationNameAr": null
    }, {
        "vaccinationId": 5,
        "vaccinationName": "vacc 5",
        "vaccinationNameAr": null
    }, {
        "vaccinationId": 6,
        "vaccinationName": "vacc 6",
        "vaccinationNameAr": null
    }]
}

This is the existing json list

alreadyExist = {
    "searchList": [{
        "vaccinationId": 2,
        "vaccinationName": "vacc 2",
        "vaccinationNameAr": null,
        "defaultYn": null
    }, {
        "vaccinationId": 5,
        "vaccinationName": "vacc 5",
        "vaccinationNameAr": null,
        "defaultYn": null
    }]
}

Had to remove the existing data from the main json list, so the result will be

allData = {
    "result": [{
        "vaccinationId": 1,
        "vaccinationName": "vacc 1",
        "vaccinationNameAr": null
    }, {
        "vaccinationId": 3,
        "vaccinationName": "vacc 3",
        "vaccinationNameAr": null
    }, {
        "vaccinationId": 4,
        "vaccinationName": "vacc 4",
        "vaccinationNameAr": null
    }, {
        "vaccinationId": 6,
        "vaccinationName": "vacc 6",
        "vaccinationNameAr": null
    }]
}

How can achieve to get the new all data list by removing the existing data.

2 Answers 2

1

you can use forEach and map

 alreadyExist.searchList.forEach(function(existInfo) {
       var index = allData.searchList.map(function(e) { return e.vaccinationId;    }).indexOf(existInfo.vaccinationId);

    if(index != -1) {
        allData.searchList.splice(index, 1);
    }
})
Sign up to request clarification or add additional context in comments.

Comments

0

You may use Lodash for this purpose

allData.searchList = _.differenceBy(allData.searchList, alreadyExist.searchList, 'vaccinationId');

1 Comment

This is method that Lodash Offers. You find this on given link on Lodash. It is a utility library that offer many useful utility methods with great performance

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.