检查变量是否存在于数组的数组中
Check if variable exists in array of arrays
我需要得到数组id,并把这个id号在var pointerid = (id number from array);
我使用以下代码来存储来自元素的点击id:
jQuery('a').click(function(){
var clickedId= $(this).attr("id");
现在我想搜索如果'element': '等于' clickdid ',如果是,得到数组id。例如:
clickedId= test
wp_button_pointer_array[1] = {'element': 'test'
所以这里'element': 'test' = clickedId (test)然后给我数组id。这里的数组id是1
var wp_button_pointer_array = new Array();
wp_button_pointer_array[1] = {
'element' : 'wp-button-pointer',
'options' : {
'content': 'The HTML content to show inside the pointer',
'position': {'edge': 'top', 'align': 'center'}
}
};
wp_button_pointer_array[2] = {
'element' : 'some-element-id',
'options' : {
'content': 'The HTML content to show inside the pointer',
'position': {'edge': 'top', 'align': 'center'}
}
};
我真的不确定我理解你的问题,但这是你想做的吗?
function findElementIdByName(arr, name) {
for (var i = 0; i < arr.length; i++)
if (arr[i].element == name)
return i;
return -1; //not found
}
//example call with your data
var eleId = findElementIdByName(wp_button_pointer_array, 'some-element-id');
边注:数组索引在javascript中从0开始,你可以使用new array(),但由于javascript解释器
var wp_button_pointer_array = [
{
'element' : 'wp-button-pointer',
'options' : {
'content': 'The HTML content to show inside the pointer',
'position': {'edge': 'top', 'align': 'center'}
}
},
{
'element' : 'some-element-id',
'options' : {
'content': 'The HTML content to show inside the pointer',
'position': {'edge': 'top', 'align': 'center'}
}
}
];
$('a').on('click', function(){
var clickedId = $(this).attr("id");
for(var i = 0; i < wp_button_pointer_array.length; i++ ) {
if(wp_button_pointer_array[i].element === clickedId) {
//do staff with wp_button_pointer_array[i]
}
}
});
这将返回包含给定element
属性的索引。
function getIndexByElementId(elementArray, elementId) {
var i, len;
for(i = 0, len = elementArray.length; i < len; i++) {
if(elementArray[i].element === elementId) {
return i;
}
}
}
相关文章:
- Javascript:OnChange事件,将数组变量传递给另一个函数
- 在将csv文件中的数据分配给数组变量时增强了d3代码
- 是socket.io异步的-我需要锁定全局数组/变量吗
- 在Javascript代码中显示PHP数组变量
- 如何在不赋值数组变量的情况下读取数组数据
- 局部数组变量不是数组
- Javascript数组变量的行为不相同
- 正在尝试将Javascript数组变量分配给HTML输入值属性
- JQuery的PHP查询数组变量
- Javascript::如何将关联数组的键获取到数组变量
- 在jsp页面中获取javascript数组变量
- 输出数组变量javascript
- 如何将数组变量从Python插入到Javascript
- 将数组变量传递给函数的下一部分
- 使用 AJAX 调用将数组变量从 JS 传递到 PHP
- d3 本地存储比较数组 .filter 咖啡脚本;数组变量不起作用
- 我们可以更新一个javascript数组变量,而不使用ajax重新加载页面吗?
- 无法使用 JavaScript 将值读入全局数组变量
- 作为“名称”的Javascript数组变量不起作用
- 如何将数组变量合并为对象数组变量