我想用另一个对象替换对象$ scope.users的数组中的元素。 我当时就想,这样做:对象在数组中但indexOf找不到dit
步骤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");
什么是$ scope.users?什么是myValue? – Donal 2014-09-03 11:04:44
你能用一个实际场景编辑你的问题:输入 - >预期输出? – Wawy 2014-09-03 11:11:27
我添加了一些数据结构 – Eagle1 2014-09-03 19:47:02