Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript array object compare to get unlisted array result

I have 2 arrays object below:

  1. var data: The complete data
  2. var choose: The only choose
var data = [
    {
        "day": "Mon",
        "id": "SC0001",
        "deviceID": "DA0002",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "day": "Wed",
        "id": "SC0001",
        "deviceID": "DA0002",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "day": "Fri",
        "id": "SC0001",
        "deviceID": "DA0002",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "day": "Sat",
        "id": "SC0001",
        "deviceID": "DA0002",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "day": "Sun",
        "id": "SC0001",
        "deviceID": "DA0002",
        "time": "08:00",
        "trigger": "ON"
    }
];

var choose = [
    {
        "deviceID": "DA0002",
        "id": "SC0001",
        "day": "Mon",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "deviceID": "DA0002",
        "id": "SC0001",
        "day": "Wed",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "deviceID": "DA0002",
        "id": "SC0001",
        "day": "Sat",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "deviceID": "DA0002",
        "id": "SC0001",
        "day": "Sun",
        "time": "08:00",
        "trigger": "ON"
    }
];

for(var i=0; i<data.length; i++) {
        var day = data[i].day;
        var scheduleID = data[i].id;
        var deviceID = data[i].deviceID;
        
        console.log(day);
        
    //How to get Friday array here?
}

As We can see above if compare with both array object, I need to get the unlisted result.
So how can I get the array object for Friday only?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use Array::filter() to filter your data array and use Array::some() in the filter callback to check that the current item’s day doesn’t exist in the choose array.

It works if day is unique per an object in an array, otherwise you should compare several unique fields.

const result = data.filter(item => !choose.some(item2 => item.day === item2.day));
console.log(result);
<script>
var data = [
    {
        "day": "Mon",
        "id": "SC0001",
        "deviceID": "DA0002",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "day": "Wed",
        "id": "SC0001",
        "deviceID": "DA0002",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "day": "Fri",
        "id": "SC0001",
        "deviceID": "DA0002",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "day": "Sat",
        "id": "SC0001",
        "deviceID": "DA0002",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "day": "Sun",
        "id": "SC0001",
        "deviceID": "DA0002",
        "time": "08:00",
        "trigger": "ON"
    }
];

var choose = [
    {
        "deviceID": "DA0002",
        "id": "SC0001",
        "day": "Mon",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "deviceID": "DA0002",
        "id": "SC0001",
        "day": "Wed",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "deviceID": "DA0002",
        "id": "SC0001",
        "day": "Sat",
        "time": "08:00",
        "trigger": "ON"
    },
    {
        "deviceID": "DA0002",
        "id": "SC0001",
        "day": "Sun",
        "time": "08:00",
        "trigger": "ON"
    }
];
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading