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
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>
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With