DEV Community

Jin Choo
Jin Choo

Posted on • Updated on

What are Arrays - Part III

arrays

Array.map()

You can change one array into another using the .map(callback) method.

For example:

const numbers = [5, 4, 7, 10]

const tripled = numbers.map(function(number) {
    return number * 3;
});
console.log(tripled); // [15, 12, 21, 30]
Enter fullscreen mode Exit fullscreen mode

and

const furBabyNames = ['dutches', 'monty', 'benny', 'lucky', 'chili'];
const furBabyUpperNames = furBabyNames(function(furBabyName){
    return furBabyName.toUpperCase();
});
console.log(furBabyNames); // ['DUTCHES', 'MONTY', 'BENNY', 'LUCKY', 'CHILI'];
Enter fullscreen mode Exit fullscreen mode

Array.includes(item)

The Array .includes(item) method is one of the simplest array methods. This is because instead of a callback it takes an element and returns true if that element is in the array and false otherwise.

const fruits = ['Grapes', 'Mango', 'Watermelon'];

fruits.includes('Mango'); // true
fruits.includes('Pineapple'); // false
Enter fullscreen mode Exit fullscreen mode

Array.join()

The join() method concatenates all of the elements in an array (or an array-like object), separated by commas or a specified separator string, to create and return a new string. If the array only contains one item, that item will be returned without the separator.

const furBabies = ['Dutches', 'Monty', 'Benny', 'Lucky', 'Chili'];
furBabies.join('; '); // 'Dutches; Monty; Benny; Lucky; Chili'
furBabies.join(' . '); // 'Dutches . Monty . Benny . Lucky . Chili'
Enter fullscreen mode Exit fullscreen mode

Top comments (0)