简体   繁体   English

在lodash中是否存在比较两个对象数组的情况,如果结果相同则不会显示

[英]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 我有两个json类型的数据“A”和“B”,它们都有相同类型的类别

{
"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 ? 是否有使用lodash功能? if there's same model of vehicle in "A" comparing into "B" the model will not be shown 如果在“A”中有相同型号的车辆,则与“B”相比,该型号将不会显示

Using native Javascript, you can use array#filter and array#some . 使用本机Javascript,您可以使用array#filterarray#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 : 这是一个lodash解决方案:

 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 你可以使用_.isEqual但你必须确保外部数组已经排序

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 : 也可以使用ES6,你可以这样:

array2.filter(e => !array1.includes(e));

Not in lodash but in plain javascript. 不是在lodash中,而是在普通的javascript中。 The following code takes var A as what you've provided in A and likewise for B 以下代码将var A视为您在A提供A ,同样也是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. notInB中的值应包含A中不在B ,顾名思义。

EDIT 编辑

I've provided a jsfiddle to illustrate this: https://jsfiddle.net/cattails27/zvc53j5g/ 我已经提供了一个jsfiddle来说明这一点: https ://jsfiddle.net/cattails27/zvc53j5g/

为什么不只是做一个普通的循环并使用_.find如果模型存在?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM