How to divide an array into two?

1

How to divide this array into two independent arrays of whatever length. Between the two arrays they must contain all the students, without any of them repeating themselves. Thanks

var alumnos =["Carlos","Clara","Cristina","Fernando","Ivan","Lara","Mercedes","Rafael","Roberto","Sandra","Tania","Raulito","Milagros"];
    
asked by fran 18.12.2017 в 18:58
source

4 answers

1
 let alumnos = ['Carlos','Clara','Cristina','Fernando','Ivan','Lara','Mercedes',
               'Rafael','Roberto','Sandra','Tania','Raulito','Milagros']
 let m1 = alumnos.splice(0,(alumnos.length/2));
 console.log("Mitad 1 --> ",m1);
 let m2 = alumnos.splice(0,alumnos.length);
 console.log("Mitad 2 -->",m2);

I hope this serves you, greetings.

    
answered by 18.12.2017 в 19:11
1

Try:

var alumnos = [1,2,3,4]
var index = alumnos.length;
var primeraParte = alumnos.slice(0, index/2);
var segundaParte = alumnos.slice(index/2,index);
console.log(primeraParte);
console.log(segundaParte);
    
answered by 18.12.2017 в 19:48
1

The total of your array is odd and you want to divide it into two, therefore one half will have more elements, you can use the splice() method to remove elements from an array. By the way, it makes sure to declare the array correctly, in this case an array of strings:

var alumnos =['Carlos','Clara','Cristina','Fernando','Ivan','Lara','Mercedes','Rafael','Roberto','Sandra','Tania','Raulito','Milagros'];


document.writeln("Total de elementos en array: " + alumnos.length + "<br>");
var mitad1 = alumnos.splice(0,alumnos.length/2);
document.writeln("Primera mitad: " + mitad1 + "<br>");

var mitad2 = alumnos.splice(0,alumnos.length);
document.writeln("Segunda mitad: " + mitad2 + "<br>");
    
answered by 18.12.2017 в 20:24
1

You have two options, depending on whether you want to keep the original array intact or not:

The slice method does not modify the original array, creating copies of a part of it:

let alumnos =["Carlos","Clara","Cristina","Fernando","Ivan","Lara","Mercedes","Rafael","Roberto","Sandra","Tania","Raulito","Milagros"];

let mitad = Math.floor(alumnos.length / 2);

let inicio = alumnos.slice(0, mitad);
let final = alumnos.slice(mitad); // si no se indica el índice final, se usa la longitud del array como referencia

console.log('Primera parte:', inicio.toString())
console.log('Segunda parte', final.toString())
console.log('Array original', alumnos.toString());

The splice method modifies the original array, extracting a part of it:

let alumnos =["Carlos","Clara","Cristina","Fernando","Ivan","Lara","Mercedes","Rafael","Roberto","Sandra","Tania","Raulito","Milagros"];

let mitad = Math.floor(alumnos.length / 2);

console.log('Redondeamos hacia abajo, la mitad está en la posición', mitad);

let inicio = alumnos.splice(0, mitad);

console.log(inicio.toString()); //primera parte, extraída
console.log(alumnos.toString()); //array original, modificado, sólo tiene la parte final
    
answered by 24.12.2018 в 10:40