-3
const elementeSelected = ["a", "b"];
const allElements = ["m", "s", "e", "r", "q", "d", "a", "b", "c"];

// result allElements = ["a", "b", "m", "s", "e", "r", "q", "d", "c"];

https://codesandbox.io/s/distracted-lederberg-yji13f?file=/src/index.js:0-175

I tried to reorder the items form allElement. I want the items from elementeSelected to be move from their position in allElements to the first position.

DenisPaul
  • 3
  • 3

2 Answers2

1

You need to concat the arrays together and remove the ones that were already used. You can do that with spread syntax and with filter. The filter uses includes to check if it is duplicated.

const elementeSelected = ["a", "b"];
const allElements = ["m", "s", "e", "r", "q", "d", "a", "b", "c"];

const result = [...elementeSelected, ...allElements.filter(n => !elementeSelected.includes(n))];

console.log(result);
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

var allelements = ["m", "s", "e", "r", "q", "d", "a", "b", "c"];
var x= 6;
var pos=0;
var temp=allelements[x];
var i;
for (i=x; i>=pos; i--)
    {
       allelements[i]=allelements[i-1];
    }
allelements[pos]=temp;

var y= 7;
var pos2=1

<!-- begin snippet: js hide: false console: true babel: false -->
var temp2=allelements[y];
var i;
for (i=y; i>=pos2; i--)
    {
       allelements[i]=allelements[i-1];
    }
alllements[pos2]=temp2;
console.log(allelements);