Skip to content

Instantly share code, notes, and snippets.

@hossainlab
Created September 30, 2018 16:39
Show Gist options
  • Save hossainlab/cdb2857ad16978503e119e68b998d74f to your computer and use it in GitHub Desktop.
Save hossainlab/cdb2857ad16978503e119e68b998d74f to your computer and use it in GitHub Desktop.
// ! Inro To Array in JS
/*
All Arrays are untyped,so you can put everything you want in an Array.
In addition to objects, the array data is ordered by indexes.
Arrays are actually objects, having the indexes (numbers from 0 to legnth - 1) as keys.
*/
// Creating Simple Array
let names = ['adib', 'sajib', 'jubayer', 'abir'];
console.log(names);
console.log(typeof names);
// Another way
var names = new Array('abir', 'adib', 'jubayer', 'sajib');
console.log(names);
console.log(typeof names);
// All Arrays are untyped,so you can put everything you want in an Array.
let mixed = [true, false, null, 'Jubayer', 123];
console.log(mixed);
console.log(typeof mixed[0]);
console.log(typeof mixed[2]);
console.log(typeof mixed[3]);
console.log(typeof mixed[4]);
// Arrays in ES6 way
Array.of() is used to create arrays instead of the array
constructor
let A1 = Array.of(1,2,3);
console.log(A1);
//Create an object into an array
let A2 = Array.from('Word');
let A1 = Array.of(1,2,3);
console.log(A2);
// You can use Array.from to manipulate values
let A3 = Array.from(A1, (value) => value*2);
console.log(A3);
// Iterate over values
let A1 = Array.of(1,2,3);
let A2 = Array.from('Word');
let A3 = Array.from(A1, (value) => value*2);
for (let v of A1) {
console.log(`A1 value: ${v}`);
}
for (let v of A2) {
console.log(`A2 value: ${v}`);
}
for (let v of A3) {
console.log(`A3 value: ${v}`);
}
// ! Some Array Methods
let names = ['abul', 'babul', 'kabul', 'dabul', 'ebul'];
// length
let l = names.length;
console.log(l);
// indexOf
let indexOfThirdElement = names.indexOf('kabul');
console.log(indexOfThirdElement);
// Last index of the array
let lastIndex = names.length -1
console.log(lastIndex);
// splice
let sp = names.splice(2, 3)
console.log(sp);
// Array indexing
let firstElement = names[0];
console.log(firstIndex);
// push(element), insert an element at the end of the array
names.push('abir');
console.log(names);
// pop, removes an element at the end of the array
names.pop();
console.log(names);
// unshift(element), insert an element at the starting point of the array or index 0
names.unshift('Adib');
console.log(names);
// shift(), remove an element at the index of 0
names.shift()
console.log(names);
// concat , combined two arrays
let ArrayOne = [2, 3,4];
let ArrayTwo = [0, -2, 3];
let ArrayThree = ArrayOne.concat(ArrayTwo);
console.log(ArrayThree);
// join() and split()
/*
The join() method combines all the elements of an array into a single string,separated by a specified delimiter.
If the delimiter is not specified, it is set to a comma.
The split() is the opposite and splits up the contents of a string as elements of an array, based on a specified delimiter.
*/
// join()
let arr = ['a', 'b', 'c'];
let joined = arr.join();
console.log(joined);
let arr = ['a', 'b', 'c'];
let joined = arr.join('-');
console.log(joined);
let arr = ['a', 'b', 'c'];
let joined = arr.join('*');
console.log(joined);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment