Arrays in JS. Example open

Arrays in JS. Example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

Arrays are created to work with sets of data. Arrays can contain terms, numbers, objects, and other arrays. Square brackets [ ] are used to create an array. Inside the square brackets, the elements of the array are defined.

        var cars = ['Ford', 'Mazda', 'Kia', 'BMW']//let's create an array using square brackets
        console.log(cars)//our array is output to the console
        console.log(cars[2])//let's turn to the element of the array. The first element has the number 0

        console.log(cars.length)//we will find out the number of elements in the array

        cars.push('Audi')//add an element to the end of the array
        cars.unshift(audi)//adds an element to the beginning of the array
        var audi = cars.pop()//removes the last element and returns it

        var ford = cars.shift()//removes the first element returns it 

        var index = cars.indexOf('Kia')//index of the element in the array
If we try to access an element at an index greater than the size of the array, then we get undefined

by index, values are set for array elements:

        const people = ["Tom", "Alice", "Sam"];//let's create an array and write it in a variable
        console.log(people[0]); //0 element of array "Tom"
        people[0] = "Bob";//assign a new value to element 0 of the array

it is possible to install an element that is not initially installed:

        console.log(people[7]); 

Multidimensional arrays

Arrays can be one-dimensional or multidimensional. Each element in a multidimensional array can be separate array:

        const numbers1 = [0, 1, 2, 3, 4, 5];//one-dimensional array
        const numbers2 = [[0, 1, 2], [3, 4, 5]];//two-dimensional array

To get an element inside a nested array, we need to use its second dimension:

        console.log(numbers2[0][1]);//1

We can also do an assignment:

        numbers2[0][1] = 5;//0 array and 1 element in it is now equal to 5

When creating multi-dimensional arrays, we are not limited to only two-dimensional ones, but we can also use arrays of large dimensions

Array Object

Arrays in JavaScript are represented by the Array object. The Array object exposes a number of properties and methods, with the help of which we can manipulate the array and its elements.

An array can be created with standard square brackets or by creating an Array object:

        const users = new Array();//same
        const users2 = [];//same

        const users = new Array("Bilbo", "Aragorn", "Gimley");//you can immediately fill the array

Another way to initialize arrays is the Array.of() method – it takes elements and initializes them array:

        const people = Array.of("Bilbo", "Aragorn", "Gimley");

Array.from function

        Array.from(arrayLike)

arrayLike – some object that is a set of symbols, this. It can be another array, string, etc.

For example, let’s pass the string “Hello” to the Array.from function:

var array = Array.from("Hello");//string
console.log(array); // ["H", "e", "l", "l", "o"] - the function will split the string into separate characters and create an array from them

The Array.from function can also take a second parameter – this is a modifier function that processes our array and returns it the result of its processing:

        var numbers = [1, 2, 3, 4];//create an array
        const array = Array.from(numbers, n => n * n);//n - element of the array
        console.log(array); // [1, 4, 9, 16]

As a result, Array.from() returns a new array, which will contain the squares of the numbers from the numbers array.

And another version of the Array.from() function takes a conversion function as the second parameter, into which in addition to an element from the iterated set, the index of this element is also passed:


const array = Array.from({ length: 3, "0": "Tom", "1": "Sam", "2": "Bob" }, (element) => {
console.log(element);
return element;
});
console.log(array); // ["Tom", "Sam", "Bob"]

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation