简体   繁体   中英

javascript access key value pair from a string

In the following string how can i access the elements For ex:

var s= "{4:123,5:456}" 
for(var i=0;i<s.split(",").length;i++)
{
        console.log(s[i][0]) // should give me 4,5
        console.log(s[i][1]) // should give me 123,456

}

I think the code should be like:

   var s= {4:123,5:456} 
   for(var entry in s)
   {
        console.log(entry); // should give me 4,5
        console.log(s[entry]); // should give me 123,456
   }

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