2016-11-06 46 views
0

我有帶有users_id數組的JSON文件。在這個陣列上,我有活動用戶,所以這個數字是不連續的。我如何檢查(在jQuery上)職位數users_id = 18(answer = 3)?接下來我想從其他字符串讀取值,以便我可以閱讀json.data_todo_counter [3]。JS和JSON - 如何查找位置號碼

{ 

    "users_id": [1, 2, 3, 18, 24], 

    "data_chat_timeread": [0, 0, 0, 0, 0], 
    "data_chat_timecurrent": [100, 0, 0, 0, 0], 
    "data_chat_counter": [4, 0, 0, 0, 0], 

    "data_todo_counter": [2, 0, 0, 0, 0] 

} 
+1

要找到18,解析JSON,然後在該特定數組上使用數組'.indexOf()'方法。 – nnnnnn

回答

1

input並且如果發現keyMemberfunction搜索該key,它返回的valueMember元件在發現index

function getSomething(input, key, keyMember, valueMember) { 
    var index = input[keyMember].indexOf(key); 
    if (index >= 0) { 
     return input[valueMember][index]; 
    } else { 
     /not found 
    } 
} 

實例:

getSomething({ 

    "users_id": [1, 2, 3, 18, 24], 

    "data_chat_timeread": [0, 0, 0, 0, 0], 
    "data_chat_timecurrent": [100, 0, 0, 0, 0], 
    "data_chat_counter": [4, 0, 0, 0, 0], 

    "data_todo_counter": [2, 0, 0, 0, 0] 

}, 18, "users_id", "data_todo_counter");