skip to Main Content

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?

2

Answers


  1. 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>
    Login or Signup to reply.
  2. I’m assuming that you want to filter away all the entries from choose from data. A basic solution would be to use Array.filter(), which runs a function over all items and only keeps the ones where a truthy value was returned:

    data.filter(datum=>!choose.includes(datum));
    

    However, the code above will not work properly here because the data in data, while having the same representation in code, will never be equal (i.e. ===) that in choose because of how JS handles objects – objects will only be considered "equal" if they are equal by reference:

    let o1 = {};
    let o2 = {};
    let o3 = o1;
    o1 === o2; // false
    o3 === o2; // false
    o3 === o1; // true
    

    In this case, you will need a deep-equals comparison, which looks at the individual keys and values of objects. A quick hack would be comparing two objects’ JSON.stringify() representations:

    data.filter(datum1 =>
      !choose.some(dataum2 => JSON.stringify(datum1) == JSON.stringify(datum2))
    );
    

    However you can also to look into using libraries, such as deep-equal:

    const deepEqual = require('deep-equal');
    data.filter(datum1 => !choose.some(dataum2 => deepEqual(datum1, datum2)));
    

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search