Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查数组的数组中是否存在变量_Javascript_Jquery_Arrays - Fatal编程技术网

Javascript 检查数组的数组中是否存在变量

Javascript 检查数组的数组中是否存在变量,javascript,jquery,arrays,Javascript,Jquery,Arrays,我需要获取数组id并将此id号放入var pointerid=(数组中的id号) 我使用此代码存储来自元素的剪辑id: jQuery('a').click(function(){ var clickedId= $(this).attr("id"); 现在我想搜索“element”:“等于clickedId”,如果是,则获取数组id。 例如:单击edid=testwp\u按钮\u指针\u数组[1]={ 'element':'test'所以这里的'element':'test'=单击EDI

我需要获取数组id并将此id号放入var pointerid=(数组中的id号)
我使用此代码存储来自元素的剪辑id:

jQuery('a').click(function(){
    var clickedId= $(this).attr("id");
现在我想搜索“element”:“等于clickedId”,如果是,则获取数组id。 例如:
单击edid=test
wp\u按钮\u指针\u数组[1]={ 'element':'test'
所以这里的'element':'test'=单击EDID(测试),然后给我数组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'} 
     }
};
var wp\u按钮\u指针\u数组=[
{
“元素”:“wp按钮指针”,
“选项”:{
'内容':'要在指针内显示的HTML内容',
'位置':{'edge':'top','align':'center'}
}
},
{
'element':'some element id',
“选项”:{
'内容':'要在指针内显示的HTML内容',
'位置':{'edge':'top','align':'center'}
}
}
];
$('a')。在('click',function()上{
var clickedId=$(this.attr(“id”);
对于(变量i=0;i
我不确定我是否理解你的问题,但这就是你想要做的吗

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');
函数findElementIdByName(arr,name){
对于(变量i=0;i

旁注:在javascript中,数组索引从0开始,您可以使用new array(),但是[]稍快一些(平均comp约80ms),因为javascript解释器中的词法分析将返回包含给定
元素的索引

function getIndexByElementId(elementArray, elementId) {
    var i, len;
    for(i = 0, len = elementArray.length; i < len; i++) {
        if(elementArray[i].element === elementId) {
            return i;
        }
    }
}
函数getIndexByElementId(elementArray,elementId){ 变量i,len; 对于(i=0,len=elementArray.length;i
在JavaScript技术说明中,新数组分配使用的是
..=[];
,而不是
..=new array();
,在本例中应该是
wp\u button\u pointer\u array=[{…},{…}]
因为JavaScript的基本语法允许包含内容的数组。通常不鼓励先创建一个空数组,然后填充它,除非填充是在动态代码中进行的。您还跳过了索引0。@Mike'Pomax'Kamermans
new array()
可以使用,并被视为普通数组。区别在于它使用构造函数表示法和文字表示法,因此
数组对象
不是
数组
。虽然可能是因为规范允许,但该语言有专门的语法用于数组构造,以及
新数组()的使用
不建议使用(不是“坏”,只是不建议使用-与新数组(大小)不同,新数组通常是坏的,除非您需要创建一个预先指定长度的数组,然后立即折叠)
function getIndexByElementId(elementArray, elementId) {
    var i, len;
    for(i = 0, len = elementArray.length; i < len; i++) {
        if(elementArray[i].element === elementId) {
            return i;
        }
    }
}