【发布时间】:2015-09-25 14:58:36
【问题描述】:
我有两个数组。这是第一个:
$scope.selection = {
"carrots",
"celery",
"corn",
"apples",
"bananas"
};
这是第二个:
$scope.shipment = [{
"id": "0",
"name": "vegetables",
"manifest": [{"carrots", "celery", "corn"}]
}, {
"id": "1",
"name": "produce",
"manifest": [{"apples", "carrots", "bananas"}]
}];
当我遍历第一个数组时,我希望能够查看第二个数组中是否存在匹配项。到目前为止,我可以使用 jQuery inArray来匹配第二个数组中的索引项:
if ($.inArray($scope.shipment.manifest[0], $scope.selection) < 0) { console.log($scope.shipment.id) };
// for "carrots"
=> "0"
但由于“carrots”在货件数组中的两个索引位置,上面只会返回第一个货件id。
我怎样才能同时获得这两个货物?
【问题讨论】:
-
您可以在 javascript 中使用 .filter 函数。这将返回与您的表达式匹配的所有元素
标签: javascript jquery arrays angularjs