JS object auto-sorting

Keys in object will be auto-sorting into an increasing numeric order:

    let obj = {};
    obj[1] = 1;
    obj[10] = 10;
    obj[2] = 2;
    console.log(JSON.stringify(obj)); // {"1":1,"2":2,"10":10}

Even if the keys are strings, sorting will be performed in order of numbers, just like above:

    let obj = {};
    obj['1'] = 1;
    obj['10'] = 10;
    obj['2'] = 2;
    console.log(JSON.stringify(obj)); // {"1":1,"2":2,"10":10}

猜你喜欢

转载自blog.csdn.net/u014510460/article/details/82393418
今日推荐