重複している可能性がありますが、見つかりませんでした。したがって、オブジェクトの配列が2つあります。
var a = [{id: '1', name: 'bob'}, {id: '2', name: 'bill'}]
$scope.b = [{id: '4', name: 'jack'}, {id: '2', name: 'bill'}, {id: '1', name: 'bob'}, {id: '3', name: 'john'}]
bからすべてのa要素を削除したい。私が試してみました:
$scope.b = $scope.b.filter(function(item){
return a.indexOf(item) === -1;
});
残念ながら、何らかの理由で、インデックスは常に-1であるため、何も削除されません。いくつかのconsole.log-sで
console.log(item);
console.log(a);
console.log(a.indexOf(item));
、これはデータがどのように見えるかです:
Resource {id: 4, name: "jack"}
[Resource, Resource, Resource, Resource, $promise: Promise, $resolved: true]
-1
あなたはこのようにそれを行うことができます
// get all id's from a
var a = [{id: '1', name: 'bob'}, {id: '2', name: 'bill'}].map(function (el) {
return el.id;
});
// search item.id in array with id's
$scope.b = $scope.b.filter(function(item){
return a.indexOf(item.id) === -1;
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加