美文网首页
js map的key排序

js map的key排序

作者: cjlynn | 来源:发表于2021-08-18 16:48 被阅读0次
sortMap(map, ascOrDesc = true) {
      let keys = new Array()
      for (var key of map.keys()) {
        keys.push(key)
      }
      if (ascOrDesc) {
        keys.sort(function (a, b) {
          if (a.length == b.length) {
            return a.localeCompare(b);
          } else {
            return a.length - b.length;
          }
        })
      } else {
        keys.sort(function (a, b) {
          if (a.length == b.length) {
            return b.localeCompare(a);
          } else {
            return b.length - a.length;
          }
        })
      }
      let newMap = new Map()
      keys.forEach(key => {
        newMap.set(key, map.get(key))
      })
      return newMap;
    },

相关文章

网友评论

      本文标题:js map的key排序

      本文链接:https://www.haomeiwen.com/subject/flngbltx.html