Array's Methods Part 2.

Array's Methods Part 2.

As we talked about unshift, shift, push, pull and splice in the previous article, now we're going to talk about some other methods in points:

  • Array has Some methods that we use to sort it:-

  1. sort():sorts the array's eles alphabetically or increasingly :
  2. reverse():reascend the array in reverse:

let arrTest = [5, 4, 3, 7, 1]
console.log(arrTest.sort()) // [1,3,4,5,7]
let arrTest = [5, 4, 3, 7, 1]
console.log(arrTest.reverse()) // [1,7,3,4,5]
let arrTest = [5, 4, -1, 3, 7, -2, 1]
console.log(arrTest.sort())
console.log(arrTest.sort((a,b) => a - b)) // [1,3,4,5,7]
console.log(arrTest.sort((a,b) => b - a)) // [7,5,4,3,1] => sort().reverse()
arrTest = ['d', 'a', 'c']
console.log(arrTest.sort()) // ['a', 'c', 'd']
console.log(arrTest.reverse()) // ['d', 'c', 'a'        

  • Some methods that make it more Easy to deal with them by indexes or values:

  1. indexof():returns the first Value's index it meets, it takes 2 args (the value we are searching for, the starting idx(opt.)), so if we have an array with duplicate values it returns the first one's idx.
  2. lastIndexOf():this works better with arrays that have duplicate values, it's same as indexOf() but returns the last idx of the value

PS: if we entered a value that's not in the array, they'll return -1

let arrTest = [5, 4, 3, 7, 3, 1, 3, 8]
console.log(arrTest.indexOf(3)) // 2
console.log(arrTest.lastIndexOf(3)) // 6
console.log(arrTest.indexOf(55)) // -1        

  • includes():this method assure if a the array contains a certain value or not. takes 2 args too (value, start idx) , returns true if exists, false otherwise.

search for find() and filter() methods too .
console.log(arrTest.includes(4)) // true

console.log(arrTest.filter((element) => element > 4)) // any element that elligeble for the condition 
// if the element doesn't exist it returns empty array []
console.log(arrTest.find((element) => element > 5)) // the first element that's elligible for the cond
// if the element doesn't exist it returns undefined
        

  • concat():merges arrays in one array.

arr1 = [1,2,3]
arr2 = [4,2,5]
arr3 =['a', 'b', 'c']
console.log(arr1.concat(arr2, arr3)) // [ 1, 2, 3, 4, 2, 5 ]        

  • slice():returns a portion of the array, takes two args (startIdx, endIdx'not included') which means it slices till it gets to the idx before the endidx:

arr = [1,2,3,4,5,6,7,8]
console.log(arr.slice(0,3)) // [1,2,3] => endidx is n't includded

arrCopy = arr.slice(0, -1) 
console.log(arrCopy) [1,2,3,4,5,6,7,8]        

  • join():this methods returns a string representation for the array's values. it works the opposite of Array.from() we talked about in creating arrays , takes one arg as the separator, its default separator is comma.

arrTest = ['k','h','o','l','o','u','d']
console.log(arrTest.join('-')) // k-h-o-l-o-u-d
console.log(arrTest.join('')) // kholoud

let n = arrTest.join('')

console.log(Array.from('mohamed')) // ['m', 'o', 'h', 'a', 'm', 'e', 'd']

console.log('mohamed'.split('')) // =>strtok() // ['m', 'o', 'h', 'a', 'm', 'e', 'd']
        

  • every():this method iterates over every ele in the array and test if the condition of the function passed all of it or not, if yes returns true, otherwise returns false. [kind if checking if ele1 true && ele2 true && ...etc]
  • some():some() method works same as every() method but it returns true if some of its eles passed the condition and false if NONE of them passed it [kind if checking if ele1 true || ele2 true || ...etc]

arrTest = [2,4,6,7]
console.log(arrTest.every((number) => number % 2 === 0)) // false
console.log(arrTest.some((number) => number % 2 === 0)) // true        

  • map():this method is one of the commonly used method in arrays, it returns a new array with the result of calling a function on every element in the array, which means it just pushes the result of passing a condition to a new array.

arrTest = [1,2,3,4]
mapRes = arrTest.map((el) => el * 2)
console.log(mapRes) // [2, 4, 6, 8]

arrTest = [10,21,30,60]

console.log(arrTest.map((number) => number > 50 ? number * 2 : number % 2 ===0? number / 2 : number)) //?[5, 21, 15, 120]        

  • forEach():this method is like your arm in coding in js using arrays :D, it iterates over every eles of the array and executes the function passed into it.

arr = ['a', 'b', 'c'];

arr.forEach((element) => console.log(element)); // a
                                                // b
                                                // c        

PS: those methods and most of the Array's methods are called 'Iterative methods' which means they take a callback function(mostly the arrow function) as argument and mostly this function is called for every element in the array, it determines the return value of the entire method.


Now i think i mentioned the widely most used array's methods, and you can check the various methods array has through this great site, it was a great resource for me to get deep understanding of array's methods. : Array - JavaScript | MDN (mozilla.org)

Thank You For Your Time!.

Abdallah Edrees

Frontend Developer | Vue.js, Nuxt.js

1 å¹´

Great efforts ??

Nada Ahmed

Frontend Developer @Startup defenders | Next.js | Node.js | Angular+2

1 å¹´

Great job ??

Abeer Mosaad

Software Engineer @_VOIS | Ex-Software Engineer Intern @alx_africa

1 å¹´

Kholoud rocks :) ??

要查看或添加评论,请登录

Kholoud Fattem的更多文章

  • First Time in Team Project? Read this - extra: Agile Methodology, Simplified

    First Time in Team Project? Read this - extra: Agile Methodology, Simplified

    What is Agile? Imagine you're building a Lego castle with friends. Instead of planning every brick and step beforehand,…

    6 条评论
  • System Design - Simplified

    System Design - Simplified

    The Initial Phase: Requirements Gathering - It's the process of brainstorming to figure out what a software or…

    36 条评论
  • ALX's Journey, Egypt's Community Power.

    ALX's Journey, Egypt's Community Power.

    A-Team "Missing Husam and Momen" This is a shoutout to all my peers, friends in COHORT13 and to myself :) ps. Iam…

    38 条评论
  • What happens when you type google.com in your browser and press Enter? -ALX

    What happens when you type google.com in your browser and press Enter? -ALX

    so at first, what's the meaning of the URL (google.com) we type in the browser? well this URL is the address of the web…

    24 条评论

社区洞察

其他会员也浏览了