排序后替换数组中的值

时间:2019-01-17 14:12:18

标签: javascript arrays sorting object

我以正确的顺序对json数据进行排序,但是我想替换位置的值。

我的json数据:


    {
      "dev@be-activ-it.fr-Quiz-10": {
        "id": "dev@be-activ-it.fr-Quiz-10",
        "user_id": "dev@be-activ-it.fr",
        "myposition": 1,
        "points": 35
      },
      "fbl@be-activ-it.fr-Quiz-10": {
        "id": "fbl@be-activ-it.fr-Quiz-10",
        "user_id": "fbl@be-activ-it.fr",
        "myposition": 2,
        "points": 38
      },
      "ddd@be-activ-it.fr-Quiz-10": {
        "id": "ddd@be-activ-it.fr-Quiz-10",
        "user_id": "ddd@be-activ-it.fr",
        "myposition": 3,
        "points": 37
      }
    }

我想要什么:


    {
      "fbl@be-activ-it.fr-Quiz-10": {
        "id": "fbl@be-activ-it.fr-Quiz-10",
        "user_id": "fbl@be-activ-it.fr",
        "myposition": 1,
        "points": 38
      },
      "ddd@be-activ-it.fr-Quiz-10": {
        "id": "ddd@be-activ-it.fr-Quiz-10",
        "user_id": "ddd@be-activ-it.fr",
        "myposition": 2,
        "points": 37
      },
      "dev@be-activ-it.fr-Quiz-10": {
        "id": "dev@be-activ-it.fr-Quiz-10",
        "user_id": "dev@be-activ-it.fr",
        "myposition": 3,
        "points": 35
      }
    }

我的代码:

let data = fs.readFileSync('course.json/rank.json', 'utf8');
    data = JSON.parse(data);

    const comparePoints = (a,b)=>{

      let a_points = a["points"]
      let b_points = b["points"]

      return b_points-a_points
    }

    const sort = (data) => {
    let keys = Object.keys(data)
    let dataAsArray = keys.map(e=>data[e])
    return dataAsArray.sort(comparePoints)
    }

    data = sort(data);
    console.log(data);

我得到的是


    [ { "id": "fbl@be-activ-it.fr-Quiz-10",
        "user_id": "fbl@be-activ-it.fr",
        "myposition": 2,
        "points": 38 },
      { "id": "ddd@be-activ-it.fr-Quiz-10",
        "user_id": "ddd@be-activ-it.fr",
        "myposition": 3,
        "points": 37 },
      { "id": "dev@be-activ-it.fr-Quiz-10",
        "user_id": "dev@be-activ-it.fr",
        "myposition": 1,
        "points": 35 } ]

我想调换职位,您能帮我吗?

3 个答案:

答案 0 :(得分:2)

使用object values提取对象的值,sort然后使用reduce重建对象:

const json = {
  "dev@be-activ-it.fr-Quiz-10": {
    "id": "dev@be-activ-it.fr-Quiz-10",
    "user_id": "dev@be-activ-it.fr",
    "myposition": 2,
    "points": 35
  },
  "fbl@be-activ-it.fr-Quiz-10": {
    "id": "fbl@be-activ-it.fr-Quiz-10",
    "user_id": "fbl@be-activ-it.fr",
    "myposition": 1,
    "points": 38
  },
  "ddd@be-activ-it.fr-Quiz-10": {
    "id": "ddd@be-activ-it.fr-Quiz-10",
    "user_id": "ddd@be-activ-it.fr",
    "myposition": 3,
    "points": 37
  }
};

const result = Object.values(json).sort((a, b) => a.myposition - b.myposition).reduce((all, curr) => {
  all[curr.id] = curr;
  return all;
}, {});

console.log(result);

答案 1 :(得分:0)

基本上,您的代码中的t1.rows不是另一个对象,因此您需要从已排序的数组中重新创建该对象。 这可以通过对每个或任何循环函数使用reduce来完成。我认为reduce更加容易看到。

在这里您使用t2.rows并返回Array

what you get

答案 2 :(得分:0)

const comparePoints = (a,b)=>{

  let a_points = a["points"]
  let b_points = b["points"]

  return a_points-b_points
}

代替b-a尝试a-b。访问:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort