Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过数组angularjs查找对象的索引_Javascript_Angularjs_Arrays - Fatal编程技术网

Javascript 通过数组angularjs查找对象的索引

Javascript 通过数组angularjs查找对象的索引,javascript,angularjs,arrays,Javascript,Angularjs,Arrays,例如,我有一个数组: $scope.records = [ { "Name" : "Alfreds Futterkiste", "Country" : "Germany" }, { "Name" : "Berglunds snabbköp", "Country" : "Sweden" }, { "Name" : "Centro comercial Moctezuma", "Coun

例如,我有一个数组:

  $scope.records = [
    {
      "Name" : "Alfreds Futterkiste",
      "Country" : "Germany"
    },
    {
      "Name" : "Berglunds snabbköp",
      "Country" : "Sweden"
    },
    {
      "Name" : "Centro comercial Moctezuma",
      "Country" : "Mexico"
    },
    {
      "Name" : "Ernst Handel",
      "Country" : "Austria"
    }
  ];
如何从对象中获取值索引?例如“国家”:“奥地利”这个索引是3,你可以在最新的浏览器中使用,但是在Internet Explorer中不支持,只有Edge

let$scope={};
$scope.records=[
{
“姓名”:“阿尔弗雷德·富特基斯特”,
“国家”:“德国”
},
{
“姓名”:“Berglunds snabbköp”,
“国家”:“瑞典”
},
{
“名称”:“商业中心Moctezuma”,
“国家”:“墨西哥”
},
{
“名称”:“恩斯特·汉德尔”,
“国家”:“奥地利”
}
];
let index=$scope.records.findIndex(record=>record.Country===“奥地利”);

console.log(索引);//3
您可以使用
array.findIndex
进行此操作:

var d=[{
“姓名”:“阿尔弗雷德·富特基斯特”,
“国家”:“德国”
}, {
“姓名”:“Berglunds snabbköp”,
“国家”:“瑞典”
}, {
“名称”:“商业中心Moctezuma”,
“国家”:“墨西哥”
}, {
“名称”:“恩斯特·汉德尔”,
“国家”:“奥地利”
}];
var searchCountry=“奥地利”
var指数=d.findIndex(x=>x.Country==searchCountry)

console.log(index)
您可以执行以下操作:返回国家,然后查找国家

$scope.records.map((item) => { return item.Country;}).indexOf('Austria')
上述代码的问题在每个元素中迭代一次以生成映射,然后找到索引。您可以使用一个简单的for循环:

var index = (arr, k, v) => { 
   var i = -1;len = (arr || []).length;  
   for (i = 0; i < len; i++) { 
     if (arr[i][k] === v) 
       return i;
   }
   return -1;
} 
var指数=(arr,k,v)=>{
变量i=-1;len=(arr | |[])。长度;
对于(i=0;i
我注意到很多findIndex解决方案。请注意:FindIndex方法已添加到ECMAScript 6规范中,根据MDN,可能还不能在所有JavaScript实现中使用


您可以使用polyfill for findIndex()并使用其他人建议的findIndex解决方案。如果您不想使用polyfill,您可以使用我作为解决方案放置的map和indexOf。

使用for-in循环,因为它是一个JSON数组,您可以这样做

for(i in records) {
if(records[i].country=="Austria") 
console.log("index is :"+ parseInt(i)); // print index
}

我注册了这个方法,效果很好

Array.prototype.getIndexByValue = function (name, value) {
        for (var i = 0, len=this.length; i <len; i++) {
            if (this[i][name]) {
                if (this[i][name] === value) {
                    return i
                }
            }
        }
        return -1;
    };

var d = [{
  "Name": "Alfreds Futterkiste",
  "Country": "Germany"
}, {
  "Name": "Berglunds snabbköp",
  "Country": "Sweden"
}, {
  "Name": "Centro comercial Moctezuma",
  "Country": "Mexico"
}, {
  "Name": "Ernst Handel",
  "Country": "Austria"
}];


var index = d.getIndexByValue('Country','Austria');
console.log(index)//will print 3
Array.prototype.getIndexByValue=函数(名称、值){

对于(var i=0,len=this.length;我建议您可以稍微修改它以供使用。至少在您找到索引后,它会停止迭代,同样的浏览器支持也是如此。@ste2425-我可以,我甚至可以添加一个ternary,您在真实情况下不会返回任何内容。这与
返回false
一样好,循环不会在第一次匹配时结束。我建议,
obj.Country=='Austria'&&(index=i);return index!=-1
@Rajesh-它之所以有效,是因为赋值是真实的,如果它是
0
以上的任何数字,如果它是
0
,那么它是第一个,可以直接返回->我不知道这个黑客行为。很好。你正在将变量
I
泄漏到窗口范围。如果找不到该值,则返回-1而不是null与indexOf函数保持一致。这应该是一个简单的web搜索
记录
已经是一个Javascript对象。您不需要
JSON.parse
。另外
typeof i
字符串
,而不是索引函数中预期的数字。
for(i in records) {
if(records[i].country=="Austria") 
console.log("index is :"+ parseInt(i)); // print index
}
Array.prototype.getIndexByValue = function (name, value) {
        for (var i = 0, len=this.length; i <len; i++) {
            if (this[i][name]) {
                if (this[i][name] === value) {
                    return i
                }
            }
        }
        return -1;
    };

var d = [{
  "Name": "Alfreds Futterkiste",
  "Country": "Germany"
}, {
  "Name": "Berglunds snabbköp",
  "Country": "Sweden"
}, {
  "Name": "Centro comercial Moctezuma",
  "Country": "Mexico"
}, {
  "Name": "Ernst Handel",
  "Country": "Austria"
}];


var index = d.getIndexByValue('Country','Austria');
console.log(index)//will print 3