Could you please help to convert this array:
['John', 'Paul', 'George', 'Ringo']
using this array:
[3, 1, 2, 0]
to this:
['Ringo', 'Paul', 'George', 'John']
Thanks a million!
Could you please help to convert this array:
['John', 'Paul', 'George', 'Ringo']
using this array:
[3, 1, 2, 0]
to this:
['Ringo', 'Paul', 'George', 'John']
Thanks a million!
You can map your array of indexes to their corresponding values from your name array using .map():
const names = ['John', 'Paul', 'George', 'Ringo']
const nums = [3, 1, 2, 0];
const res = nums.map(i => names[i]);
console.log(res);
const arr1 = ["John", "Paul", "George", "Ringo"];
const arr2 = [3, 1, 2, 0];
const newArr = new Array(arr1.length).fill(0); // Creating an array in the size of arr1 with all values set to 0
arr1.forEach((el ,i) => { // Iterating over arr1 and getting the current element and its index
newArr[arr2[i]] = el; // Placing the current element in the needed index in the new array (arr2[i] is the needed index from arr2)
})