1

I have two arrays one is the main array wherein I have different parameters and another array which have only two parameters that matches from first array.

I want to remove the complete item from first array if similar array(matched on these two parameter) matches.

Eg:

var mainArray = [{'Name':'Ticket1','TaskId':'b5de781e-9d25-49e7-af6d-3e254e894c04','ResourceId':'977dacf0-0b61-413e-a9a4-b469ab30d1b1',
'Status':'Completed'},{'Name':'Ticket2','TaskId':'c5ae581e-9f25-49e7-af6d-3e254e894c04','ResourceId':'37fdadf1-0b61-413e-a9a4-b469ab30d1b1',
'Status':'InProgress'},{'Name':'Ticket3','TaskId':'45af551e-9f25-49e7-af6d-3e254e894c04','ResourceId':'37fdadf1-0b61-413e-a9a4-b469ab30d1b1',
'Status':'InProgress'}];

var tmpArray = [{'TaskId':'b5de781e-9d25-49e7-af6d-3e254e894c04','ResourceId':'977dacf0-0b61-413e-a9a4-b469ab30d1b1'},{'TaskId':'45af551e-9f25-49e7-af6d-3e254e894c04','ResourceId':'37fdadf1-0b61-413e-a9a4-b469ab30d1b1'}];

I want my final array to have items which are not in tmpArray with similar TaskId and ResourceId only.

i.e my final array should look like

[{'Name':'Ticket2','TaskId':'c5ae581e-9f25-49e7-af6d-3e254e894c04','ResourceId':'37fdadf1-0b61-413e-a9a4-b469ab30d1b1',
'Status':'InProgress'}]

Here the items are removed as they had matching TaskId and ResourceId in tmpArray

1
  • You can use mainArray.splice(startIndex, endIndex) to remove the items from the array. Commented Mar 15, 2016 at 16:16

1 Answer 1

2

Array#filter in combination with Array#some should help.

var mainArray = [{ 'Name': 'Ticket1', 'TaskId': 'b5de781e-9d25-49e7-af6d-3e254e894c04', 'ResourceId': '977dacf0-0b61-413e-a9a4-b469ab30d1b1', 'Status': 'Completed' }, { 'Name': 'Ticket2', 'TaskId': 'c5ae581e-9f25-49e7-af6d-3e254e894c04', 'ResourceId': '37fdadf1-0b61-413e-a9a4-b469ab30d1b1', 'Status': 'InProgress' }, { 'Name': 'Ticket3', 'TaskId': '45af551e-9f25-49e7-af6d-3e254e894c04', 'ResourceId': '37fdadf1-0b61-413e-a9a4-b469ab30d1b1', 'Status': 'InProgress' }],
    tmpArray = [{ 'TaskId': 'b5de781e-9d25-49e7-af6d-3e254e894c04', 'ResourceId': '977dacf0-0b61-413e-a9a4-b469ab30d1b1' }, { 'TaskId': '45af551e-9f25-49e7-af6d-3e254e894c04', 'ResourceId': '37fdadf1-0b61-413e-a9a4-b469ab30d1b1' }],
    result = mainArray.filter(function (a) {
        return !tmpArray.some(function (b) {
            return a.TaskId === b.TaskId && a.ResourceId === b.ResourceId;
        });
    });

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Or a solution with better perfomance/complexity and a helper object

var mainArray = [{ 'Name': 'Ticket1', 'TaskId': 'b5de781e-9d25-49e7-af6d-3e254e894c04', 'ResourceId': '977dacf0-0b61-413e-a9a4-b469ab30d1b1', 'Status': 'Completed' }, { 'Name': 'Ticket2', 'TaskId': 'c5ae581e-9f25-49e7-af6d-3e254e894c04', 'ResourceId': '37fdadf1-0b61-413e-a9a4-b469ab30d1b1', 'Status': 'InProgress' }, { 'Name': 'Ticket3', 'TaskId': '45af551e-9f25-49e7-af6d-3e254e894c04', 'ResourceId': '37fdadf1-0b61-413e-a9a4-b469ab30d1b1', 'Status': 'InProgress' }],
    tmpArray = [{ 'TaskId': 'b5de781e-9d25-49e7-af6d-3e254e894c04', 'ResourceId': '977dacf0-0b61-413e-a9a4-b469ab30d1b1' }, { 'TaskId': '45af551e-9f25-49e7-af6d-3e254e894c04', 'ResourceId': '37fdadf1-0b61-413e-a9a4-b469ab30d1b1' }],
    result = function (array1, array2) {
        var o = {};
        tmpArray.forEach(function (a) {
            o[a.TaskId + '|' + a.ResourceId] = true;
        });
        return mainArray.filter(function (a) {
            return !o[a.TaskId + '|' + a.ResourceId];
        });
    }(mainArray, tmpArray);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

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

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.