我有两个json类型的数据“A”和“B”它们都有相同类型的类别
> db.orders.createIndex({item: 1})
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
> db.orders.insert({item: undefined})
WriteResult({ "nInserted" : 1 })
> db.orders.find({item: {$type: 6}}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "temp.orders",
"indexFilterSet" : false,
"parsedQuery" : {
"item" : {
"$type" : 6
}
},
"winningPlan" : {
"stage" : "FETCH",
"filter" : {
"item" : {
"$type" : 6
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"item" : 1
},
"indexName" : "item_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"item" : [
"[undefined, undefined]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "Andys-MacBook-Pro-2.local",
"port" : 27017,
"version" : "3.2.8",
"gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
},
"ok" : 1
}
是否有使用lodash功能?如果在“A”中有相同型号的车辆,则与“B”相比,模型将不会显示
答案 0 :(得分:2)
使用原生Javascript,您可以使用array#filter
和array#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);

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

如果您想要没有汽车的车型。
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; }

这是一个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>
&#13;
答案 1 :(得分:1)
你可以使用_.isEqual
,但你必须确保外部数组已经排序
var array1 = [['a', 'b'], ['b', 'c']];
var array2 = [['b', 'c'], ['a', 'b']];
_.isEqual(array1.sort(), array2.sort()); //true
也可以使用ES6:
array2.filter(e => !array1.includes(e));
答案 2 :(得分:0)
不是在lodash中,而是在普通的javascript中。以下代码采用var A
作为您在A
中提供的内容,同样适用于B
const filterList = B.map(b => b.model);
const notInB = A.filter(a => filterList.indexOf(a.vehicle.model) < 0);
notInB
中的值应包含A
中不在B
中的值,顾名思义。
修改强>
我提供了一个jsfiddle来说明这一点:https://jsfiddle.net/cattails27/zvc53j5g/
答案 3 :(得分:-1)
为什么不只是做一个普通的for循环,如果模型存在就使用_.find
?