0

Подскажите, как сделать сортировка одного простого массива, по значениям другого? Массив для сравнения:

let array_1 = ['john', 'michael', 'hugh', 'jack', 'andrew', 'vince', 'stewie'];
let array_2 = ['jack', 'michael', 'andrew'];

Подскажите, как отсортировать так, чтобы получилось:

console.log(array_2); // ['michael', 'jack', 'andrew'];

1 ответ 1

1

let array_1 = ['john', 'michael', 'hugh', 'jack', 'andrew', 'vince', 'stewie'];
let array_2 = ['jack', 'michael', 'andrew'];

array_2.sort((a, b) => array_1.indexOf(a) - array_1.indexOf(b))
console.log(array_2)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.