我想要的是将对象 $scope.users 数组中的元素替换为另一个对象。 我想这样做:
第1步找到我要替换的对象
var myValue;
myValue = $filter('filter')($scope.users,{email: "email"});
它给了我我想要的对象
第 2 步编辑:myValue 现在包含从数组 $scope.users 中提取的确切对象,因此现在我要查找它并找到其索引。
var index = $scope.users.indexOf(myValue);
这会返回-1,所以错误...
$scope.users 就是这样
[Object { id=10, username="Eagle1", username_canonical="eagle1", more...}, Object { id=11, username="Pedro", username_canonical="pedro", more...}, Object { id=12, username="Carine", username_canonical="carine", more...}, Object { id=16, username="stephane", username_canonical="stephane", more...}, Object { id=17, username="throwError", username_canonical="throwerror", more...}]
过滤后 myValue 是这样的:
[Object { id=17, username="throwError", username_canonical="throwerror", more...}]
编辑:我是这样解决的:
var index = $scope.users.map(function(e){return e.keytofind}).indexOf("keytofind");
最佳答案
我认为它是这样的:
var elementPos = array.map($filter('filter')($scope.users,{email: "email"}))
.indexOf(idYourAreLookingFor);
var objectFound = array[elementPos];
关于javascript - 该对象在数组中,但indexOf找不到dit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25642653/