获取数组索引的值

时间:2013-11-29 14:00:18

标签: javascript jquery

我有一个ajax响应数组,我希望得到索引的值。

  

[{ “UserLatitude”: “33.7543”, “UserLongitude”: “ - 84.3744”} { “UserLatitude”: “22.6962”, “UserLongitude”: “75.8651”},{ “UserLatitude”: “22.6963”,” UserLongitude “:” 75.8654 “},{” UserLatitude “:” 37.7858" , “UserLongitude”: “ - 122.406”},{ “UserLatitude”: “0”, “UserLongitude”: “0”},{ “UserLatitude”: “37.7858”, “UserLongitude”: “ - 122.406”},{ “UserLatitude”: “37.7858”, “UserLongitude”: “ - 122.406”},{ “UserLatitude”: “37.7858”, “UserLongitude”: “ - 122.406” },{ “UserLatitude”: “0”, “UserLongitude”: “0”},{ “UserLatitude”: “0”, “UserLongitude”: “0”},{ “UserLatitude”: “22.6962”, “UserLongitude” : “75.8653”},{ “UserLatitude”: “22.6963”, “UserLongitude”: “75.8654”},{ “UserLatitude”: “0”, “UserLongitude”: “0”},{ “UserLatitude”: “33.7543” “UserLongitude”: “ - 84.3745”},{ “UserLatitude”: “0”, “UserLongitude”: “0”},{ “UserLatitude”: “0”, “UserLongitude”: “0”},{“UserLatitude “:” 33.7543" , “UserLongitude”: “ - 84.3744”}]

2 个答案:

答案 0 :(得分:1)

您在第一个导致错误的数组对象后丢失了逗号。否则result[0].UserLatitude将起作用 - jsfiddle

答案 1 :(得分:0)

您需要迭代数组,然后检查这些值是否匹配

$.each (arr, function (index, value) { 
    if(value["UserLatitude"] == "33.7543" && value["UserLongitude"] == "-84.3744") {
        console.log(index);
        return false;
    }
});

如果找到匹配项,请打印 index 并退出循环。

Fiddle