gpt4 book ai didi

javascript - 在对象中查找下一个键 id

转载 作者:行者123 更新时间:2023-11-30 12:26:39 25 4
gpt4 key购买 nike

我将一个对象存储在一个变量中(考虑为数据表)。

var data=[{"controlID":"A","currentValue":"10","onChange":"","onClick":""},
{"controlID":"B","currentValue":"5","onChange":"Testing(A,B)","onClick":""},
{"controlID":"C","currentValue":"-5","onChange":"Testing1(A,B)","onClick":""},
{"controlID":"D","currentValue":"","onChange":"Testing2(B,C)","onClick":""},{"controlID":"E","currentValue":"","onChange":"Testing3(C,D)","onClick":""},{"controlID":"F","currentValue":"","onChange":"","onClick":""}];

现在我知道第二行键值为 B。如何获取第三行(即“C”行值)

我是这个领域的新手。请帮助我们。

最佳答案

此函数将返回您的索引:

var FindIndexOfControlID = function(id, data){
for(var i = 0; i < data.length ; i++){
if( data[i]['controlID'] == id ){
return i;
}
}
};

用法:

var index = FindIndexOfControlID('C', data);

实例

http://jsfiddle.net/urahara/medhgm7b/


注意

Alternatively you may also want to implement function that returns index of any specified property and value:

var FindIndexOfProperty = function(value, property, data){
for(var i = 0; i < data.length ; i++){
if( data[i][property] == value ){
return i;
}
}
};

Usage

FindIndexOfProperty('-5', 'currentValue',data); // returns 2

关于javascript - 在对象中查找下一个键 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29118954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com