-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
  • 100,966
  • 191
  • 140
  • 197
Ricky
  • 114
  • 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
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

Use slice

var r = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let data = r.slice(0, 7);
console.log(data)
brk
  • 48,835
  • 10
  • 56
  • 78
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);
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103