-3
data=[];
r=[1,2,3,4,5,6,7,8,9];
data.push(r.splice(7,1));

At the end I don't need the 8,9, I need

data=[1,2,3,4,5,6,7]
Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
Ricky
  • 94
  • 1
  • 10

3 Answers3

2

You can just shallow copy with slice:

r = [1,2,3,4,5,6,7,8,9]
data = r.slice(0,-2)

Example:

var r = [1,2,3,4,5,6,7,8,9];
var data = r.slice(0,-2);
console.log(data);
T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
Bhojendra Rauniyar
  • 78,842
  • 31
  • 152
  • 211
0

Use slice

var r = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let data = r.slice(0, 7);
console.log(data)
brk
  • 46,805
  • 5
  • 49
  • 71
0

You can try like this way also, with r.length - 2 it just shorten your array length by 2 so here it is discarding last two elements.

r = [1, 2, 3, 4, 5, 6, 7, 8, 9];
r.length = r.length - 2;
console.log(r);
Always Sunny
  • 32,751
  • 7
  • 52
  • 86