Basic Data Structures Flashcards
Cards, resources and exercises of Data Structures in Javascript
What Are Basic data structures in Javascript ?
Variable, Arrays and Objects
How to add item at the beginning of an array ?
let arr = [1,2,3,4];
arr.unshift(0);
//result 0,1,2,3,4
Obs.: unshift method accepts one or more parameters
How to add item at the end of an array ?
let arr = [0 ,1,2,3,4];
arr.push(5);
//result 0,1,2,3,4,5
Obs.: push method accepts one or more parameters
How to remove item at the beginning of an array ?
let arr = [0,1,2,3,4,5];
arr.shift();
//result 1,2,3,4,5
Obs.: doens’t accepts parameters
Obs.: returns element removed
How to remove item at the end of an array ?
let arr = [0,1,2,3,4,5];
arr.pop();
//result 0,1,2,3,4
Obs.: doens’t accepts parameters
Obs.: returns element removed
How to remove itens at the middle of an array ?
let arr = [2, 5, 1, 5, 2, 1]; let removed = arr.splice(2, 2);
// sum elements arr.reduce((a, b) => a + b);
//result 10
Obs.: returns an array with elements removed
How to change values of an array using splice ?
let arr = [“eu”, “gosto”, “de”, “praia”];
arr.splice(3, 1, “parque”);
//results [“eu”, “gosto”, “de”, “parque”]
How to retrieve a slice of an array ?
let animals = [“dog”, “cat”, “giraffe”, “crocodile”, “elephant”];
let pets = animals .slice(0,2);
//results pets ==> ["dog","cat"] // results ==> animals ["dog", "cat", "giraffe", "crocodile", "elephant"]
Obs.: slice doesn’t change the source array.
How to copy an entire array without ref and without change it?
let jsDataTypes = [‘boolean’,’undefined’,’number’, ‘null’,’string’, ‘symbol’];
let data = […jsDataTypes];
//results data ==> [‘boolean’,’undefined’,’number’, ‘null’,’string’, ‘symbol’]
How to Combine Arrays with the Spread Operator ?
let japanCarBrand = [‘toyota’,’honda’,’nissan’];
let usaCarBrand = [‘ford’, ‘chevrolet’,’GMC’];
let franceCarBrand = [‘renault’, ‘citroen’,’pegeout’];
let carBrands = […japanCarBrand, …usaCarBrand, …franceCarBrand];
//results [‘toyota’,’honda’,’nissan’,’ford’, ‘chevrolet’,’GMC’,’renault’, ‘citroen’,’pegeout’]
How to check if an alement exists in the array ?
let names = [‘sandra’, ‘julia’, ‘meryl’, ‘angelina’,’scarlet’];
let index = names.indexOf(‘julia’);
//results 1
How to through all itens of an array with basic for loop ?
let decades = [1970,1980,1990,2000,2010];
for (let i = 0; i < decades.length; i++){ console.log(decades[i]); } //results 1970, 1980, 1990, 2000, 2010
How to create complex multi-dimensional arrays ?
let myNestedArray = [ // change code below this line [ [ 'terceiro nível', [ 'quarto nível', [ 'quinto nível', 'deepest' ], 'deeper' ], 'deep' ] ] ];
console.log(myNestedArray[0][0][1][1][1]); //results deepest
How to create an object in Javascript ?
let foods = { apples: 25, oranges: 32, plums: 28 };
How to add properties to an existing object ?
let foods = { apples: 25, oranges: 32, plums: 28 };
//add new properties
foods. bananas = 13;
foods. grapes = 35;
foods. strawberries = 27;
Obs.: If the property doesn’t exist yet, is created with the value passed;