// Есть массив var a = ["1","2","3","4"]; // Есть массив индексов, по которому он сортируется и выводится: var b = [1,0,3,2]; var newArr = []; for(var i = 0; i < b.length; i++){ newArr.push(a[b[i]]); } // Получается newArr == ["2","1","4","3"] // Я хочу передвинуть в новом массиве newArr значения "3" на первое место. // Получился такой массив. ["3","2","1","4"] // Вопрос, как поменять массив a, не трогая массив b, чтобы получился такой массив ["3","2","1","4"] ? 

I will add one more example:

 Есть множество значений (массив) A = ["Имя","Фамилия","Отчество","Должность"]; Есть множество позиций перемещения этих значений B = [1,0,3,2] Есть множество D, которое создаётся из значений множества A, размещенных в позиции из множества B D = ["Фамилия","Имя","Должность","Отчество"] ("Фамилия" индекс 1 , "Имя" индекс 0, "Должность" индекс 3 , "Отчество" индекс 2) - соответствие множеству B Но нужно отсортировать множество D вот так ["Имя","Отчество","Фамилия","Должность"] Но имея возможность изменять лишь множество A, а не B. Множество B константно. Множество D создаётся из и A и B. 
  • Do you need newArr and a to have the same values? - Vasiliy Rusin
  • where are you going to change the array a before it zaforitis or after - L11VADIS
  • No, array a should not be equal to array newArr. It is necessary to swap the elements in the array a, so that after moving these elements, after the cycle for the array, the newArr will take the value ["3", "2", "1", "4"]. - manking
  • You need to understand what kind of algorithm it is and which branch of mathematics solves this problem. - manking

1 answer 1

I recommend to reconsider the approach to solving your problem in favor of associative arrays with named keys

instead of divination that where and how goes under the number N, in your example

("Last name" index N, "First name" index N, "Position" index N, "Middle name" index n, ...)

use a construction where each key corresponds to a certain value

 var obj = { "imay": "Петя", "familiya": "Петров", "doljnost": "Босс", "otchestvo": "Батьковичь" // и т.д. } 

in this approach, it does not matter in what sequence the occurrences of properties go.

you can retrieve the data in any desired order directly by referring to the object

 var result = obj.imay + " " + obj.otchestvo + " " + obj.doljnost + " " + obj.familiya 

by calling a function with its object passed as an argument

 function fu_00a1(_obj){ return _obj.imay + " " + _obj.otchestvo + " " + _obj.doljnost + " " + _obj.familiya } // alert(fu_00a1(obj)) function fu_00a2(_obj){ return _obj.familiya+ " " + _obj.doljnost + " " + _obj.otchestvo + " " + _obj.imay } // alert(fu_00a2(obj)) 

when creating a named object, you can transfer values ​​to its values ​​from the numbered Array array.

 var A = ["Имя","Фамилия","Отчество","Должность"]; var obj = { "imay": A[0], "familiya": A[1], "doljnost": A[3], "otchestvo": A[2] // и т.д. } 

Then, using functions or using direct access, create new arrays

  function fu_00a3(_obj){ return [_obj.imay, _obj.otchestvo,_obj.doljnost, _obj.familiya] } // alert(fu_00a3(obj)) 

__ for a change __

Here is a reference to public where I give an example of creating a method of correcting the position of elements in a numbered array

how to javascript to swap the elements of the array Array

The very solution of your math

 // исходный массив var A = ["Возраст","Фамилия", "Должность", "Отчество", "Пол", "Имя"]; // массив условия var condArr = [5,1,3,2] // новый массив var newArr = [] for(var i = 0; i < condArr.length; i++){ newArr[i] = A[condArr[i]] } 

PS I think this is more a section of computer science than mathematics))