lodash 中是否有比较两个对象数组,如果结果相同,则不会显示这些对象数组

Mik*_*ria 6 javascript lodash

我有两个 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"
        }]
}
Run Code Online (Sandbox Code Playgroud)

是否有一个 lodash 函数可以使用?如果“A”中有相同型号的车辆与“B”比较,则不会显示该型号

Has*_*mam 5

使用本机 Javascript,您可以使用array#filterarray#some

在这里它将获取模型数组中没有模型的汽车。

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);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)

如果您想要没有汽车的车型。

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);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)

这是一个 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);
Run Code Online (Sandbox Code Playgroud)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Run Code Online (Sandbox Code Playgroud)
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js'></script>
Run Code Online (Sandbox Code Playgroud)