jazzedge
7/28/2017 - 11:16 AM

Bot - Examples of using arrays in node/javascript

Bot - Examples of using arrays in node/javascript

'use strict';
var myData = []; 
// w  ww. j a va  2  s.  c  o  m
myData.push(1); // add at the end 
console.log(myData); // prints [1] 

myData.unshift(2); // add to the top 
console.log(myData); // prints [2,1] 

// Arrays are zero index based: 
console.log(myData[0]); // prints 2 

// The array type in JavaScript length property returns the element count:
var arr2 = [];
arr2.length;
console.log(arr2);
var arr3 = [ 'cat', 'rat', 'bat' ];
console.log(arr3.length);

// By default, arrays in JavaScript are numerically indexed:
var arr3 = [ 'cat', 'rat', 'bat' ];
for (var i = 0; i < arr3.length; i++) {
    console.log(arr3[i]);
}

//To delete an item from an array, use the splice function, which takes an index and the number of items to delete:
var arr3 = [ 'cat', 'rat', 'bat', 'cat', 'rat', 'bat' ];
arr3.splice(2, 2);
console.log(arr3);
console.log(arr3.length);

//The push and pop functions let you add and remove items to the end of an array, respectively:
var nums = [ 1, 1, 2, 3, 5, 8 ];
nums.push(13);
console.log(nums);
nums.pop();
console.log(nums);

//To insert or delete items from the front of an array, use unshift or shift , respectively:
var nums = [ 1, 2, 3, 5, 8 ];
nums.unshift(1);
console.log(nums);
nums.shift();
console.log(nums);

//The array function join returns a string from the array:

var nums = [ 1, 1, 2, 3, 5, 8 ];
var s = nums.join(", ");
console.log(s);

//You can sort arrays using the sort function, which can be used with the built-in sorting function:

var nums = [ 3, 1, 8, 5, 2, 1];
nums.sort();
console.log(nums);

//We can provide your own sorting function as a parameter:

var names = [ 'CSS', 'HTML', 'Java', 'SQL', 'CSS3', 'HTML5'];
names.sort();/*  w w w .  j a  va  2 s  . co m*/
console.log(names);
names.sort(function (a, b) {
         var a1 = a.toLowerCase(), b1 = b.toLowerCase();
         if (a1 < b1) return 1;
         if (a1 > b1) return -1;
         return 0;
});
console.log(names);

//To iterate over items in arrays, we can use the for loop or forEach function

[ 'CSS', 'HTML', 'CSS3', 'HTML5', 'Javascript', 'SQL'].forEach( function  (value) {
   console.log(value);
});


//Array of objects
//Make an array of the book object
var books = [];
var new_book = {id: "book1", name: "twilight", category: "Movies", price: 10};
books.push(new_book);
new_book = {id: "book2", name: "The_call", category: "Movies", price: 17};
books.push(new_book);
console.log(books[0].id);
console.log(books[0].name);
console.log(books[0].category);
console.log(books[0].price);
console.log(books[1].id);
console.log(books[1].name);
console.log(books[1].category);
console.log(books[1].price);
console.log(books.length); //Returns 2


//Now add variables to an object
var v1 = "Alpha";
var v2 = "Beta";
var v3 = "Gamma";
var v4 = "42";
new_book = {"id":v1, "name":v2, "category":v3, "price":v4};
console.log(new_book);

books.push(new_book);
console.log(books[2].id);
console.log(books[2].name);
console.log(books[2].category);
console.log(books[2].price);
console.log(books.length); //Returns 3