简体   繁体   中英

loop through a javascript array

I have a javascript array with this structure :

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! 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:

$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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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