Javascript

Home Load Console Selectors CSS Form Strings If Else Array Reg Exp Date & Time setTimeout JSON Loops Objects Animation Fat Arrow Class Import IIFE undefined Tabulator.js ES6 Event Listeners AJAX Hide Elements Create Elements Checkbox ejs
O O

Weather Controls

Time Of Day
Rain
Wind Speed
Wind Direction
Clouds

Javascript : Array

2022-07-18

Array Basics

var fruits = "Banana,Orange,Apple,Mango";
var arrFruit = ["Banana", "Orange", "Apple", "Mango"];

// add to array
arrFruit.push("Kiwi");

// convert string to array
var arr = fruits.split(',');

// convert array to string
var str = arrFruit.toString(); 

Array Sort

String Sort (alphabetical)

var food = ["Banana", "Chocalte", "Apple", "Del Taco"];
food.sort();            
console.log(food)  // Array [ "Apple", "Banana", "Chocalte", "Del Taco" ]

//reverse sort
fruits.reverse(); 

Number Sort

var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});

//reverse
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return b - a});