我有一个 ajax jquery 函数,它用每个包含标签和值的元素填充一个数组:
var accountOptions = new Array();
$.ajax({
type: "GET",
url: apiUrl+"/Accounts/GetDropdown",
async: false,
success:
function (response) {
$.each(response.Data, function (i, item) {
accountOptions.push({ label: item.Number, value: item.Id })
});
}
});
此函数运行良好,但我需要通过在数组中搜索来找到给定标签(帐号)的值 (Id)。最有效的方法是什么?
感谢任何帮助。
最佳答案
使用filter函数,在 javascript 中
// options is an array, if you're sure the result is only one use options[0].value, if not you will have to iterate it
var options = accountOptions.filter(function(account) {
return account.label === 'label-you-are-looking-for';
});
关于javascript - 通过给定标签在 javascript 数组中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33355016/