2014-09-03 25 views
0

我想用另一个对象替换对象$ 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"); 
+0

什么是$ scope.users?什么是myValue? – Donal 2014-09-03 11:04:44

+0

你能用一个实际场景编辑你的问题:输入 - >预期输出? – Wawy 2014-09-03 11:11:27

+0

我添加了一些数据结构 – Eagle1 2014-09-03 19:47:02

回答

1

我认为它是这样的:

var elementPos = array.map($filter('filter')($scope.users,{email: "email"})) 
         .indexOf(idYourAreLookingFor); 
var objectFound = array[elementPos]; 
+0

假设“$ scope.users”是数组。 – Margus 2014-09-03 11:11:58

+0

如果$ scope.users不是一个数组我会有JS错误不是吗? – Eagle1 2014-09-03 14:33:41

+0

不是我所需要的。我发现解决方案在其他地方,但你的实际上是正确的 – Eagle1 2014-09-06 17:07:06