我正在努力对一组对象进行相当复杂的操作。首先这是一个示例数组:
[
{id: 11, item: 132, lineNum: 4, linePosition: 5}
{id: 2, item: 124, lineNum: 0, linePosition: 2}
{id: 9, item: 130, lineNum: 0, linePosition: 5}
{id: 12, item: 133, lineNum: 1, linePosition: 3}
{id: 15, item: 136, lineNum: 3, linePosition: 2}
{id: 13, item: 134, lineNum: 0, linePosition: 4}
{id: 10, item: 131, lineNum: 2, linePosition: 1}
{id: 8, item: 129, lineNum: 3, linePosition: 3}
{id: 1, item: 123, lineNum: 0, linePosition: 5}
{id: 3, item: 125, lineNum: 1, linePosition: 7}
{id: 5, item: 127, lineNum: 0, linePosition: 9}
{id: 7, item: 128, lineNum: 1, linePosition: 2}
{id: 4, item: 126, lineNum: 0, linePosition: 4}
{id: 14, item: 135, lineNum: 3, linePosition: 9}
]
所以我想尽可能高效地按顺序进行以下操作。按 lineNum 对数组进行排序,然后按项目升序排序。然后我想修改每个对象,lineNum === 0使 linePosition 从 0 开始递增。所以第一个匹配对象的 linePosition 为 0,下一个是 1,然后是 2,等等。
结果数组如下所示:
[
{id: 1, item: 123, lineNum: 0, linePosition: 0}
{id: 2, item: 124, lineNum: 0, linePosition: 1}
{id: 4, item: 126, lineNum: 0, linePosition: 2}
{id: 5, item: 127, lineNum: 0, linePosition: 3}
{id: 9, item: 130, lineNum: 0, linePosition: 4}
{id: 13, item: 134, lineNum: 0, linePosition: 5}
{id: 3, item: 125, lineNum: 1, linePosition: 7}
{id: 7, item: 128, lineNum: 1, linePosition: 2}
{id: 12, item: 133, lineNum: 1, linePosition: 3}
{id: 10, item: 131, lineNum: 2, linePosition: 1}
{id: 8, item: 129, lineNum: 3, linePosition: 3}
{id: 14, item: 135, lineNum: 3, linePosition: 9}
{id: 15, item: 136, lineNum: 3, linePosition: 2}
{id: 11, item: 132, lineNum: 4, linePosition: 5}
]
潇湘沐
ABOUTYOU
相关分类