1

Been looking at arr.splice() but that does not seem to be what I'm after. Basically, I need to "splice" a range, ie:

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

I need to return a new array with either:

const newA = [1,2,3]
const newB = [4,5,6]
const newC = [7,8,9]

Surely arr.splice() can achieve the first and last but how to achieve something like newB?

In plain english, "remove the first n index and also the n last index in the array (arr) and give me the rest" which should be newB.

Sylar
  • 11,422
  • 25
  • 93
  • 166
  • Does that help you? https://stackoverflow.com/questions/26357805/how-to-delete-the-last-element-from-an-array-java and that https://stackoverflow.com/questions/3663944/what-is-the-best-way-to-remove-the-first-element-from-an-array – Jean Lucas Monte Carvalho Jan 19 '18 at 17:30
  • I have no knowledge of Java, sorry. – Sylar Jan 19 '18 at 17:30
  • Related [add and remove specific array elements using array.splice()](https://stackoverflow.com/questions/47419025/add-and-remove-specific-array-elements-using-array-splice) – guest271314 Jan 19 '18 at 17:46

3 Answers3

4

Yopu want slice not splice (if you want to do it in a single call).

console.log([1,2,3,4,5,6,7,8,9,10].slice(3, 6));

Also, slice returns a new array, splice just changes the original.


To clear up any ambiguity..

If you want to remove the last n items and the first y items, you might consider defining this function..

var a = [1,2,3,4,5,6,7,8,9,10];
const newA = arrayTrim(a, 0, 7);
const newB = arrayTrim(a, 3, 4)
const newC = arrayTrim(a, 6, 1);

console.log(newA, newB, newC);

function arrayTrim(array, first=0, last=0){
  last = !last ? undefined : -last;
  return array.slice(first, last)
}

If you want to start at the nth index and get y items from the array you can use this..

var a = [1,2,3,4,5,6,7,8,9,10];
const newA = arrayTrim(a, 0, 3);
const newB = arrayTrim(a, 3, 3)
const newC = arrayTrim(a, 6, 3)

console.log(newA, newB, newC);

function arrayTrim(array, first, length){
  return array.slice(first, length+first)
}
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116
  • 1
    You should use `3, -3`, since he wants the between, whatever length that is. – zfrisch Jan 19 '18 at 17:32
  • @zfrisch - not according to his exampes he doesn't. using -3 would return 3-7 while his example is 3-6... – I wrestled a bear once. Jan 19 '18 at 17:37
  • 2
    I would ask the OP but he states `In plain english, "remove the first n index and also the n last index in the array (arr) and give me the rest" which should be newB.` – zfrisch Jan 19 '18 at 17:38
  • @Iwrestledabearonce. `slice()` is not the only approach available to achieve the requirement using a single call https://stackoverflow.com/questions/48346486/how-to-remove-the-first-few-and-last-few-elements-in-an-array-and-keep-the-remai/48346529#comment83679008_48346529 – guest271314 Jan 19 '18 at 17:39
  • Yep, I just realized my mistake haha. I kept thinking about it like `.splice`. oi vey, sorry. – zfrisch Jan 19 '18 at 17:40
  • At second example `.slice(0, 0)` returns an empty array – guest271314 Jan 19 '18 at 17:49
  • @guest271314 - i thought you were being sarcastic when you added that comment... even so, that would require op to know exactly which indexes he wanted... it sounds like he wanted to simply trim the first and lest few off, which means he would have to calculate which indexes he wanted to keep and at that point it;s more than one call... i'll edit the function to account for zeros though, thanks for pointing that out... – I wrestled a bear once. Jan 19 '18 at 17:54
  • @Iwrestledabearonce. The function is currently defined with default parameters set to `0`, where `const newA = arrayTrim(a) // []` – guest271314 Jan 19 '18 at 17:58
3
arr.splice(0, 3);
arr.splice(-4); // or arr.splice(6)
console.log(arr);
guest271314
  • 1
  • 15
  • 104
  • 177
1

You want to use two arguments for arr.slice

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

arr.slice(0,3); /*returns [1,2,3]*/
arr.slice(3,7); /*returns [4,5,6,7]*/
arr.slice(7,10); /*returns [8,9,10]*/
Thatalent
  • 404
  • 2
  • 8