Difference between new Array () in ES5 and Array.of () in ES6 in JavaScript

3

When we use the Constructor new Array() in ES5 to build a new fix we do the following:

EXAMPLE 1

let elementos = new Array(2);
console.log(elementos[0]);
console.log(elementos[1]);
//ambos console darán undefined

However, what is the problem?

In the previous example, the numeric value that was passed to it is assumed with the length of the array, but each position in the same array has no elements

EXAMPLE 2

let elementos = new Array("alfa");
console.log(elementos[0]); //imprime alfa
console.log(elementos[1]); //imprime undefined

In the previous example the only element passed is not numeric but a text string, which is assumed in the zero position of the array and the next position will be undefined

EXAMPLE 3

let elementos = new Array("alfa", "BETO");
console.log(elementos[0]); //imprime alfa
console.log(elementos[1]);  //imprime BETO

In the previous example we have passed two values, not numerical; same that are placed in each position of the arrangement

How do you solve this Array.of() ?

    
asked by element 30.09.2018 в 04:01
source

2 answers

3

The similarity is that both create an instance of a Array and the significant difference between Array() and Array.of () is just the behavior of its constructor.

When we pass a single numeric value as a parameter to Array this parameter presets the length (length) of Array but with Array.of () what you expect are elements or values with which you will build the array , depending on the quantity, the length will be established. If a single numerical value passes or not this will be added to the first position.

Ejm

let array = new Array(9);
console.log(array);
console.log("Length Array " + array.length);
let arrayOf = Array.of(9);
console.log(arrayOf);
console.log("Length Array.of " +arrayOf.length);
let arrayOf2 = Array.of("Nueve");
console.log(arrayOf2);
console.log("Length Array.of " +arrayOf2.length);

That's only when a single parameter is passed, if more than one parameter is sent to the constructor of Array (the "type" does not matter) , < a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Referencia/Objetos_globales/Array#Se_crea_con"> will take into account the second constructor where it also expects all the elements of a array.

You can pass element by element, or pass the array directly using the Propagation Operator , or spread operator

Ejm

let elementos = [1,"a",4,6 , true];
let array = new Array(...elementos);
console.log(" Length Array " + array.length);
console.log(array);
let array2 = new Array(1,"a",false);
console.log(" Length Array " + array2.length);
console.log(array2);
let arrayOf = Array.of(...elementos);
console.log(" Length Array.of " + arrayOf.length);
console.log(arrayOf);
    
answered by 30.09.2018 / 08:51
source
3

With the use of Array.of() in ES6 we can do the following

EXAMPLE 1

let elementos = Array.of(12, 45);
console.log(elementos[0]);        //imprime 12     
console.log(elementos[1]);        //imprime 45

In the previous example we are passing you two values that regardless of whether they are numeric or will not be assigned as a value to each position of the array

EXAMPLE 2

let elementos = Array.of(12);
console.log(elementos[0]);    //imprime 12         
console.log(elementos[1]);    //imprime undefined

In the previous example the opposite of new Array() the only numerical value that is passed to it is not assumed as the length of the array but as the value of the first array position

EXAMPLE 3

let elementos = Array.of("67.4", false)
console.log(elementos[0])  //imprime "67.4"
console.log(elementos[1])  //imprime false

In the previous example, we can see that the first element is a text string and is assigned to position 0 and the second element is a Boolean value assigned to position 1

    
answered by 30.09.2018 в 04:01