Array map,filter,forEach...

KIMMY·2020년 4월 7일
0

codingEveryday

목록 보기
2/8

Array.map()
Array.filter()
Array.forEach()
Array.includes()
Array.push()

  1. Array.map()
    creates a new array.
  2. Array.forEach()
    executes a provided function

both execute function once for each array element. but map creates a NEW ARRAY and forEach only EXECUTES.

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]
const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.forEach(x => console.log(x * 2));

// expected output
// 2
// 8
// 18
// 32
  1. array.includes()
    The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
    -> example usage :

const pets = ['cat', 'dog', 'bat'];

if(pets.includes('cat')){
  pets.push('cat tower')
  console.log(pets);
};

// output
// Array ["cat", "dog", "bat", "cat tower"]
profile
SO HUMAN

0개의 댓글