Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  60] [ 6]  / answers: 1 / hits: 16726  / 6 Years ago, wed, august 29, 2018, 12:00:00

I am facing problem to change the index values of an array according to given index in javaScript. i have searched on StackOverflow and found some solution but those not working for me of are not for javaScript. The below input array and index is mentioned.



   Input:  arr[]   = [101, 102, 103];
index[] = [1, 0, 2];
Output: arr[] = [102, 101, 103]
index[] = [0, 1, 2]


The founded answers are
reorder array according to given index



php - sort an array according to second given array



Rearrange an array according to key



Sorting an Array according to the order of another Array



Any kind of hint/solution is highly appreciated.


More From » arrays

 Answers
186

What exactly does not work for you?





const arr = [101, 102, 103];
const index = [1, 0, 2];

const output = index.map(i => arr[i]);
console.log(output);




[#53616] Sunday, August 26, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hasanb

Total Points: 321
Total Questions: 102
Total Answers: 96

Location: Burkina Faso
Member since Fri, Sep 4, 2020
4 Years ago
;