How to rearrange an array by indices array?

前端 未结 9 1925
故里飘歌
故里飘歌 2021-02-01 19:12

Given an array arr and an array of indices ind, I\'d like to rearrange arr in-place to satisfy the given indices. For exa

9条回答
  •  盖世英雄少女心
    2021-02-01 19:48

    Try this:

    var result = new Array(5);
    for (int i = 0; i < result.length; i++) {
        result[i] = arr[ind[i]];
    }
    console.log(arr);
    

提交回复
热议问题