object index problem

Posted on
  • Hi all,

    I have encountered a strange problem regarding indices. I have a small snippet of example code that shows this problem:

    var a = {};
    var c = {};
    
    var b = "0";
    
    a[b] = "hello world";
    c["0"] = "hello moon";
    
    console.log(a[b],a[0],a["0"]);
    // output: hello world undefined undefined
    
    console.log(c[b],c[0],c["0"]);
    // output: undefined hello moon hello moon
    
    function foo(id) {
     console.log(id,a[id], c[id]);
    }
    
    foo(b);
    // output: 0 hello world undefined
    
    foo("0");
    // output: 0 hello world undefined
    

    I would expect that a[b] and a["0"] would give the same result?

    Thanks for all the help so far!

  • Thanks! Yes, that looks like a big problem. I'll look into it - and thanks for coming up with the code that demonstrates it so nicely.

    Array indices should that can be integers should be converted to ints. It looks like when you put a string in a variable, for some reason it doesn't get converted.

  • Ok, Fixed!

    It'll be in http://www.espruino.com/binaries/git/com­mits/3b5c45f55cef03c7e9cf3ba87bd57ec9a7c­536b1 in an hour or so - or 1v62 when it's released.

  • Great! Thanks!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

object index problem

Posted by Avatar for Alex @Alex

Actions