Mike Victoria
Mike Victoria

Reputation: 256

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

Upvotes: 6

Views: 13326

Answers (4)

jkris
jkris

Reputation: 6511

Not in lodash but in plain javascript. The following code takes var A as what you've provided in A and likewise for B

const filterList = B.map(b => b.model);
const notInB = A.filter(a => filterList.indexOf(a.vehicle.model) < 0);

Values in notInB should contain values from A that's not in B, as the name implies.

EDIT

I've provided a jsfiddle to illustrate this: https://jsfiddle.net/cattails27/zvc53j5g/

Upvotes: 0

Hassan Imam
Hassan Imam

Reputation: 22524

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>

Upvotes: 5

Hokutosei
Hokutosei

Reputation: 2164

why not just do a plain for loop and use _.find if model exists?

Upvotes: -1

Emad Dehnavi
Emad Dehnavi

Reputation: 3441

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));

Upvotes: 2

Related Questions