React Immutable JS如何订购地图

时间:2018-02-05 17:06:09

标签: javascript list reactjs redux immutable.js

我有一个搜索操作,在props中返回以下内容:

rank: List(...)
   0: "3"
   1: "2"
   2: "1"
   3: "4"

results: Map(...)
   0: Array[2]
      0: "2"
      1: Record(...)
         0: Array[2]
            0: "firstName"
            1: "Bob"
         1: Array[2]
            0: "lastName"
            1: "Smith"
       ...

如何按排名列表中的值订购结果地图?当我使用results.map()时,它按ID(1,2,3,4)对它们进行排序。

1 个答案:

答案 0 :(得分:2)

你应该避免混合不可变类型和javascript类型。

const Person = Record({
    lastname: null,
    firstname: null,
    id: null,
};

因此,结果现在是一个包含人物记录的地图

result = Map(...)
    35: Record(...) { id: 35, lastname: 'smith', firstname: 'bob' }
    27: Record(...) { id: 27, lastname: 'smith', firstname: 'Clara' }
    9: Record(...) { id: 9, lastname: 'dupont', firstname: 'michael' }
    3: Record(...) { id: 3, lastname: 'taylor', firstname: 'morgan' }

 const ResultOrdered = rank.map( id => result.get(id));

因此,ResultOrdered是按其等级排序的RecordPerson列表