Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there in lodash comparing two arrays of objects that will not be shown if there's same result

I have two json type of data "A" and "B" they have both same types category

{
"A":
    [{ 
    "id": "1",
       "vehicle": {
        "model": "toyota"
            }
    },{
    "id":"2",
         "vehicle": {
        "model": "vios"
            }
    },{
    "id":"3",
         "vehicle": {
        "model": "honda"
            }
    },{
    "id":"4",
         "vehicle": {
        "model": "eon"
            }
    },{
    "id":"5",
         "vehicle": {
        "model": "motor"
            }
     }]
}
---------------------------------------------------------
{
    "B":
        [{ 
            "model": "volkswagen"
        },{
            "model": "hyundai"
        },{
            "model": "honda"
        },{
            "model": "mitsubishi"
        },{
            "model": "bmw"
        }]
}

Is there's a lodash function do is use ? if there's same model of vehicle in "A" comparing into "B" the model will not be shown

like image 874
Mike Victoria Avatar asked Aug 23 '17 03:08

Mike Victoria


2 Answers

Using native Javascript, you can use array#filter and array#some.

Here it will fetch cars which have no model in your models array.

const cars = {"A":[{"id": "1","vehicle": {"model": "toyota"}},{"id":"2","vehicle": {"model": "vios"}},{"id":"3","vehicle": {"model": "honda"}},{"id":"4","vehicle": {"model": "eon"}},{"id":"5","vehicle": {"model": "motor"}}]};

const models = {"B":[{"model": "volkswagen"},{"model": "hyundai"},{"model": "honda"},{"model": "mitsubishi"},{"model": "bmw"}]};

var result = cars.A.filter((car) => {
  return !models.B.some((model) => model.model === car.vehicle.model ) 
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

In case, you want models which don't have cars.

const cars = {"A":[{"id": "1","vehicle": {"model": "toyota"}},{"id":"2","vehicle": {"model": "vios"}},{"id":"3","vehicle": {"model": "honda"}},{"id":"4","vehicle": {"model": "eon"}},{"id":"5","vehicle": {"model": "motor"}}]};

const models = {"B":[{"model": "volkswagen"},{"model": "hyundai"},{"model": "honda"},{"model": "mitsubishi"},{"model": "bmw"}]};

var result = models.B.filter((model) => {
  return !cars.A.some((car) => model.model === car.vehicle.model ) 
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Here is a lodash solution :

const cars = {"A":[{"id": "1","vehicle": {"model": "toyota"}},{"id":"2","vehicle": {"model": "vios"}},{"id":"3","vehicle": {"model": "honda"}},{"id":"4","vehicle": {"model": "eon"}},{"id":"5","vehicle": {"model": "motor"}}]};

const models = {"B":[{"model": "volkswagen"},{"model": "hyundai"},{"model": "honda"},{"model": "mitsubishi"},{"model": "bmw"}]};

var result = _.filter(models.B, (model) =>
    !_.some(cars.A, (car) => model.model === car.vehicle.model));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js'></script>
like image 60
Hassan Imam Avatar answered Oct 22 '22 04:10

Hassan Imam


you can use _.isEqual but u must make sure that the outer array is already sort

var array1 = [['a', 'b'], ['b', 'c']];
var array2 = [['b', 'c'], ['a', 'b']];
_.isEqual(array1.sort(), array2.sort()); //true

also with ES6 you can go like this :

array2.filter(e => !array1.includes(e));
like image 2
Emad Dehnavi Avatar answered Oct 22 '22 04:10

Emad Dehnavi