xxxxxxxxxx
const array = [2, 5, 9];
let squares = array.map((num) => num * num);
console.log(array); // [2, 5, 9]
console.log(squares); // [4, 25, 81]
xxxxxxxxxx
const products = [
{ name: 'Laptop', price: 32000, brand: 'Lenovo', color: 'Silver' },
{ name: 'Phone', price: 700, brand: 'Iphone', color: 'Golden' },
{ name: 'Watch', price: 3000, brand: 'Casio', color: 'Yellow' },
{ name: 'Aunglass', price: 300, brand: 'Ribon', color: 'Blue' },
{ name: 'Camera', price: 9000, brand: 'Lenovo', color: 'Gray' },
];
const productName = products.map(product => product.name);
console.log(productName);
//Expected output:[ 'Laptop', 'Phone', 'Watch', 'Aunglass', 'Camera' ]
xxxxxxxxxx
const numbers = [1, 2, 3, 4, 5];
const bigNumbers = numbers.map(number => {
return number * 10;
});
xxxxxxxxxx
const numbers = [1, 4, 9];
const roots = numbers.map((num) => Math.sqrt(num));
// roots is now [1, 2, 3]
// numbers is still [1, 4, 9]
xxxxxxxxxx
/* Answer to: "javascript map function" */
/*
<Array>.map() - One of the most useful in-built methods in JavaScript (imo).
The map() method creates a new array populated with the results of calling
a provided function on every element in the calling array.
For more information, click on the source link.
Let me make some examples of it's uses:
*/
let array = [1, 4, 9, 16];
array.map(num => num * 2); // [2, 8, 18, 32];
array.map(pounds => `£${pounds}.00`); // ["£1.00", "£4.00", "£9.00", "£16.00"];
array.map(item => Math.sqrt(item)); // [1, 2, 3, 4];
xxxxxxxxxx
var array = "M 175 0 L 326.55444566227675 87.50000000000001 L 326.55444566227675 262.5 L 175 350 L 23.445554337723223 262.5 L 23.44555433772325 87.49999999999999 L 175 0".split(" ");
let neWd = array.map(x => {
if (x === 'M' || x === 'L'){
return x;
}else{
return x * 2;
}
}).join(' ')
console.log(neWd);
xxxxxxxxxx
let arr = [1, 2, 3, 4, 5]; // map function in JS
let newArr=arr.map((item) => {
return item * item;
})
console.log(newArr)
//output
// [1,4,9,16,25]
xxxxxxxxxx
let users = [
{ firstName: "Susan", lastName: "Steward", age: 14, hobby: "Singing" },
{ firstName: "Daniel", lastName: "Longbottom", age: 16, hobby: "Football" },
{ firstName: "Jacob", lastName: "Black", age: 15, hobby: "Singing" }
];
xxxxxxxxxx
const myAwesomeArray = [5, 4, 3, 2, 1]
myAwesomeArray.map(x => x * x)
// >>>>>>>>>>>>>>>>> Output: [25, 16, 9, 4, 1]
xxxxxxxxxx
// Create a Map
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);