Docsity

Fundamentos de programación: estructuras de datos en JavaScript, Diapositivas de Programación Javascript

Vista previa parcial del texto

¡Descarga Fundamentos de programación: estructuras de datos en JavaScript y más Diapositivas en PDF de Programación Javascript solo en Docsity!

Programming fundamentals

4. Data Structures: Arrays and Objects Introduction

What we will see in this presentation

Data Sets

JavaScript provides a data type specifically for storing sequences of values. It

is called an array and is written as a list of values between square brackets,

separated by commas.

Array

var colors = ["Blue", "Orange", "Green", "Red", "Black"];

var colors = new Array("Blue", "Orange", "Green", "Red", "Black");

Array properties

Javascript

The JavaScript Array object is a global object that is used in the construction of arrays.
An array is a special type of variable that allows you to store multiple values in a single
variable.

Array properties

var colors = new Array("Blue", "Orange");

var colorsLength = colors.length; // => 2

Count Array Elements

PHP

Unlike Javascript, in PHP arrays do not have properties like length, so it is necessary

to use the native functions to be able to count the elements in the array.

$colors = ["Blue", "Orange", "Green", "Red", "Black"];

$colorsLength = count($colors); $colorsLength = sizeof($colors);

Count Array Elements

When working with arrays , there are several default methods

which we can use to modify or read the contents of arrays.

These methods are quite common among programming languages.
One of the default methods that we can use is push() , which allows us to add value to
the end of an array.

Array.push

let sequence = [1, 2, 3]; sequence.push(4); sequence.push(5); console.log(sequence); // → [1, 2, 3, 4, 5]

const cities = [ "Barcelona", "Madrid", "Lisbon" ];

cities.forEach(function (city) { console.log(city); });

// Barcelona // Madrid // Lisbon

Array.forEach

The forEach() method is used to
execute a callback function for each of
the elements in the array rather than
using a for or while loop.

Array.map

The map() method is used to execute a
callback function for each of the
elements in the array and returns a
new array with the elements changed.

const cities = [ "Barcelona", "Madrid" ];

let newCities = cities.map(function (city) { return City name is ${city}; });

// [ // "City name is Barcelona", // "City name is Madrid" // ]

const elements = ["Fire", "Air", "Water"];

console.log(elements.join()); // "Fire,Air,Water"

console.log(elements.join("")); // "FireAirWater"

console.log(elements.join("-")); // "Fire-Air-Water"

Array.join

The join() method creates and returns
a new string by concatenating all of the
elements in an array separated by
commas or a specified separator string.

let firstHalf = ["Monday", "Tuesday", "Wednesday"]; let secondHalf = ["Thursday", "Friday"];

let weekDays = firstHalf.concat(secondHalf);

console.log(weekDays); // [ // "Monday", // "Tuesday", // "Wednesday", // "Thursday", // "Friday" // ];

Array.concat

The concat() method is used to merge
two or more arrays. This method does
not change the existing arrays, but
instead returns a new array.