.slice() second element from array

my array:

var arr= ["1", "2", "3","4","5", "6", "7","8","9","10","11"];

i want to .slice() it so output will be:

var finalArr = [ "1", "2", "4", "5", "6","9","10","11"];

i used sliced like that, but how can i .slice() second element from my array?

var b = [].concat(arr.slice(0, 6), arr.slice(-3)); // output: ["1", "2", "3", "4", "5", "6", "9", "10", "11"]

i know about .splice() but that solution doesn’t work for my project

Answer

It’s a matter of the right ‘end’ value in the slicing I suppose.

const initial = ["1", "2", "3","4", "5", "6", "7", "8", "9", "10", "11"];
const finalArr = [ "1", "2", "4", "5", "6", "9", "10", "11"];

// concat the slices
const concatted = initial.slice(0, 2)
  .concat(initial.slice(3, 6))
  .concat(initial.slice(-3));

// or use the spread operator ...
const spreaded = [
  ...initial.slice(0, 2), 
  ...initial.slice(3, 6), 
  ...initial.slice(-3) ];

// or flat three sliced subarrays
const flattened = [
  initial.slice(0, 2), 
  initial.slice(3, 6), 
  initial.slice(-3) ].flat();
  
// or filter all values NOT in finalArr
const notInFinalArray = initial.filter(v => !finalArr.find(fa => fa === v));
const filtered = initial.filter( v => !notInFinalArray.includes(v) );

console.log(`concatted ${JSON.stringify(concatted)} equals finalArr? ${
  JSON.stringify(concatted) === JSON.stringify(finalArr)}`);
console.log(`spreaded ${JSON.stringify(spreaded)} equals finalArr? ${
  JSON.stringify(spreaded) === JSON.stringify(finalArr)}`);
console.log(`flattened ${JSON.stringify(flattened)} equals finalArr? ${
  JSON.stringify(flattened) === JSON.stringify(finalArr)}`);
console.log(`filtered ${JSON.stringify(filtered)} equals finalArr? ${
  JSON.stringify(filtered) === JSON.stringify(finalArr)}`);
Source: stackoverflow
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .