How to split a json list in half in javascript?

702 Views Asked by At

I have a JSON with a list of numbers from 1 to 10. How would I essentially split the list into half so that 1-5 gets saved into a new json, and 6-10 gets saved in another json?

var json = JSON.parse(body);
1

There are 1 best solutions below

0
Luan Nico On

Maybe take a look at some of these methods from Array: splice, slice ;)

E.g.:

const middle = arr.length / 2; // if it's odd, it'll round down
const first = arr.slice(0, middle);
const last = arr.slice(middle, arr.length);