Learning Goals
- Use
mapto transform values. - Use
filterto keep some values. - Use
findto get first matching value.
Working With Data
Learn how to use array methods like map and filter in Node.js. No browser needed.
map to transform values.filter to keep some values.find to get first matching value.// Save as lesson7.js
let prices = [8000, 12000, 25000, 6000];
let expensive = prices.filter(function (p) {
return p >= 10000;
});
let withTax = prices.map(function (p) {
return p * 1.18;
});
console.log(expensive); // [12000, 25000]
console.log(withTax); // [9440, 14160, 29500, 7080]
node lesson7.js in your terminal to see the output.filter to keep scores above 50.map to add 5 marks to each score.find to get first score below 40.Given product prices, create one list for cheap items and one list with VAT added.
let productPrices = [3500, 9000, 12000, 5000];
let cheapItems = productPrices.filter(function (p) {
return p <= 5000;
});
let pricesWithVat = productPrices.map(function (p) {
return p * 1.18;
});
console.log(cheapItems);
console.log(pricesWithVat);