如何让一个数组项目的索引位置控制器angularjs传递一个值 [英] how to get index position of an array item in controller passing a value in angularjs

查看:77
本文介绍了如何让一个数组项目的索引位置控制器angularjs传递一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有值列表的数组。

 [Object { id="5", country="UAE"}, Object { id="4", country="India"}]

我想获得数组元素的基础上,该ID值present索引。我怎样才能得到一个数组项的索引位置与angularJS控制器ID = 4的价值?

I want to get the index of array item based on the value present in the id. How can I get the index position of an array item with value of id = 4 in angularJS Controller?

推荐答案

这是不是angularjs具体的问题,而是正常的JavaScript。
只是循环并返回指数

this is not angularjs specific problem but normal javascript. just loop and return the index

var list =  [{ id="5", country="UAE"}, { id="4", country="India"}];

for (var i = 0; i < list.length ; i++) {
        if (list[i][id] === 4) {
            return i;
        }
 }

然后你可以使它通用通过它阵列功能,它接受的价值和属性名

you can then make it generic by making it function on array which accepts the value and property name

Array.prototype.getIndexOfObject = function(prop, value){
   for (var i = 0; i < this.length ; i++) {
            if (this[i][prop] === value) {
                return i;
            }
     }
}

这篇关于如何让一个数组项目的索引位置控制器angularjs传递一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆