JavaScript Arrays 2 Downloadable Source Code Guides code examples

Clear Array of Duplicates and empty values

const arr = [‘Laurence’,’Jack’,’Jane’,”,,,,’Sam’,’Laurence’,’Jack’,’Jane’,”,null,false,undefined,0,’Sam’];

console.log(arr);

const arr1 = [… new Set(arr)];

console.log(arr1);

const arr2 = arr.filter(Boolean);

console.log(arr2);

const arr3 = [… new Set(arr.filter(Boolean))];

console.log(arr3);

How to loop through an array

const arr = [‘Laurence’,’Linda’,’Joe’,’Jane’];

console.log(arr);

console.log(‘****FOR’);

for(let i=0;i<arr.length;i++){

   console.log(arr[i]);

}

console.log(‘****WHILE’);

let i=0;

while(i<arr.length){

   console.log(arr[i]);

   i++;

}

console.log(‘****ForEach’);

arr.forEach((item,index,array)=>{

   console.log(item);

})

console.log(‘****MAP’);

const arr1 = arr.map((item,index)=>{

   console.log(item);

   return `${index} ${item}`;

})

console.log(‘****Filter’);

const arr2 = arr.filter((item)=>{

   console.log(item);

   return item;

});

console.log(arr2);

Array Updating adding and removing array items

const arr = [‘Laurence’,’Next’];

arr.push(‘End Push’); //adds to end

const val1 = arr.shift(); //removes first

const val2 = arr.pop(); //removes last

arr.unshift(val1); // adds to start

console.log(val1);

console.log(val2);

console.log(arr);

const str1 = arr.toString();

console.log(str1);

const str2 = arr.join(‘*-*’);

console.log(str2);

JavaScript Map Method for Arrays new array

const arr = [‘Laurence’,4,545,false,’Test’];

const arr2 = arr;

arr2.push(‘NEW’);

console.log(arr);

const arr1 = arr.map((item,index,array)=>{

   const temp = `${index} ${item}`;

   return temp;

})

arr2.push(‘NEW’);

console.log(arr1);

const arr3 = [3,54,62,4334,1232,444];

const arr4 = arr3.map(val => val*2);

console.log(arr4);

const arr5 = arr3.map(callback1);

console.log(arr5);

function callback1(item){

   console.log(item);

   return item*2;

}

const arr6 = [{first:’Laurence’,last:’Svekis’},{first:’John’,last:’Smith’},{first:’Sam’,last:’Jones’}]

console.log(arr6);

const arr7 = arr6.map(({first,last})=>{

   //console.log(first,last);

   //return `${first} ${last}`;

   return {fullName:`${first} ${last}`}

})

console.log(arr7);

const arr8 = arr6.map(({first,last})=> ({full:`${first} ${last}`}));

console.log(arr8);

How to concat arrays and duplicate an array

const arr1 = [‘Laurence’,’Svekis’];

const arr2 = [1,2,3,4,5];

const arr3 = arr1.concat(arr2);

console.log(arr3);

const arr4 = arr1.concat(arr2,[‘new1′,’new2’]);

console.log(arr4);

const arr5 = arr2.concat(arr2,arr2);

console.log(arr5);

const arr6 = arr1;

arr6.push(‘END’);

const arr7 = arr1.concat();

arr6.push(‘more’);

console.log(arr7);

console.log(arr1);

console.log(arr6);

Array iterator entries using entries and looping items

const arr1 = [‘Laurence’,’Svekis’,100];

const adder = arr1.entries();

console.log(adder);

//console.log(adder.next());

//console.log(adder.next().value);

for(let item of adder){

   console.log(item[1]);

}

const adder1 = arr1.entries();

let val = adder1.next();

while(val.value){

   console.log(val.value[1]);

   val = adder1.next();

}

Array Filter Method Example for JavaScript Arrays

const arr = [‘Svekis’,’Laurence’,’test’,’new’,5,23,54,5,1213343];

const arr1 = arr.filter(val => val.length >= 4);

console.log(arr1);

const arr2 = arr.filter(checker);

console.log(arr2);

function checker(item,index,array){

   //console.log(item);

  /// console.log(index);

   //console.log(array);

   return index >=2;

}

const arr3 = arr.filter((item,index)=>{

   //console.log(typeof(item) );

   return typeof(item) == ‘string’;

});

console.log(arr3);

arr.push(null);

arr.push(false);

console.log(arr);

const arr4 = arr.filter(Boolean);

console.log(arr4);

const arr5 = arr.filter((str)=>{

   if(typeof(str)==’string’){

       const first = str[0].toUpperCase();

       return first === str[0];

   }

})

console.log(arr5);

Array includes Method and index methods JavaScript coding Example

const arr = [‘Laurence’,’Svekis’,’Svekis’,100,’Svekis’,1000];

const boo1 = arr.includes(‘Svekis’);

console.log(boo1);

const boo2 = arr.includes(‘test’);

console.log(boo2);

const ind1 = arr.indexOf(‘Svekis’);

console.log(ind1);

const ind2 = arr.indexOf(‘test’);

console.log(ind2);

const lind1 = arr.lastIndexOf(‘Svekis’);

console.log(lind1);

const lind2 = arr.lastIndexOf(‘test’);

console.log(lind2);

const find1 = arr.findIndex(call1);

console.log(find1);

const find2 = arr.findIndex(call2);

console.log(find2);

function call1(item){

   //console.log(item);

   if(item === ‘Svekis’ ){

       return item;

   }

}

function call2(item){

   if(typeof(item)==’number’){

       return item;

   }

}

Leave a Comment