简体   繁体   English

遍历一个javascript数组

[英]loop through a javascript array

I have a javascript array with this structure : 我有一个具有这种结构的javascript数组:

array('123455'=>'kjqs dkjq sdkj ','135468'=>'msldmsdlv sdml,sdmlcsdc ','16554d'=>'msljkfhsdlkjfhsmdlkfh')

I would like to loop through it without exceeding the limits , and how can I get the index and value 我想在不超出限制的情况下进行遍历,以及如何获取索引和值

thanks 谢谢

Same as Emmerman but don't forget the var keyword to avoid creating global variables! 与Emmerman相同,但不要忘记var关键字以避免创建全局变量! And add the check for own property to omit properties from prototypes. 并添加对自有属性的检查以从原型中省略属性。

var array = {
  '123455': 'kjqs dkjq sdkj ',
  '135468': 'msldmsdlv sdml,sdmlcsdc ',
  '16554d': 'msljkfhsdlkjfhsmdlkfh'
};

for (var key in array) {
  if (array.hasOwnProperty(key)) {
    console.log(key, array[key]);
  }
}

Besides this I think you're in the wrong language. 除此之外,我认为您的语言不正确。 I think it's done like this in php: 我认为这是在php中完成的:

$array = array(
  '123455' => 'kjqs dkjq sdkj ',
  '135468' => 'msldmsdlv sdml,sdmlcsdc ',
  '16554d' => 'msljkfhsdlkjfhsmdlkfh'
);

foreach ($array as $key => $value) {
  echo "\$array[$key] => $value.\n";
}
for (key in array) {
  console.log(key+' - '+array[key]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM