对象属性排序

    const obj = { a: 1, b: 2, c: 3, d: 3, e: 5, f: 1 };
    let o = {};
    Object.keys(obj)
      // 正序
      .sort((a, b) => obj[a] - obj[b])
      .map((item) => {
        o[item] = obj[item];
      });
    console.log(JSON.stringify(o, null, 4));
    const obj = { a: 1, b: 2, c: 3, d: 3, e: 5, f: 1 };
    let o = {};
    Object.keys(obj)
      // 倒序
      .sort((a, b) => obj[b] - obj[a])
      .map((item) => {
        o[item] = obj[item];
      });
    console.log(JSON.stringify(o, null, 4));

你可能感兴趣的:(对象属性排序)