JAVASCRIPT
use .map(), .filter() js
var arr = [1, 25, 20, 4];
var sum = arr.map((item) => item * 10).filter((item) => item > 100);
var sum2 = arr.map((item) => item * 10)
console.log("sum2", sum2)
// sum2 : (4) [10, 250, 200, 40]
console.log("sum", sum);
// sum : (2) [250, 200]
Map and Filter methods used together
const list = ['Apple', 'Orange', 'Egg']
list.map(item => item[0]).filter(item => item === 'A') //'A'
javavscript array.filter with array.map
var arr = [1, 25, 20, 4];
var sum = arr.map((item) => item * 10).filter((item) => item > 100);
var sum2 = arr.map((item) => item * 10)
console.log("sum2", sum2)
// sum2 : (4) [10, 250, 200, 40]
console.log("sum", sum);
// sum : (2) [250, 200]
map and filter js
var options = [
{ name: 'One', assigned: true },
{ name: 'Two', assigned: false },
{ name: 'Three', assigned: true },
];
var reduced = options.reduce(function(filtered, option) {
if (option.assigned) {
var someNewValue = { name: option.name, newProperty: 'Foo' }
filtered.push(someNewValue);
}
return filtered;
}, []);
document.getElementById('output').innerHTML = JSON.stringify(reduced);
<h1>Only assigned options</h1>
<pre id="output"> </pre>
map & filter
const filteredList = watchList.map(movie => ({
title : movie['Title'],
rating: movie["imdbRating"]
})
).filter(movie => {
return parseFloat(movie.rating) >= 8.0;
})
map&filter
const files = [ 'foo.txt ', '.bar', ' ', 'baz.foo' ];
const filePaths = files
.map(file => file.trim())
.filter(Boolean)
.map(fileName => `~/cool_app/${fileName}`);
// filePaths = [ '~/cool_app/foo.txt', '~/cool_app/.bar', '~/cool_app/baz.foo']
map&filter
const files = [ 'foo.txt ', '.bar', ' ', 'baz.foo' ];
const filePaths = files.reduce((acc, file) => {
const fileName = file.trim();
if(fileName) {
const filePath = `~/cool_app/${fileName}`;
acc.push(filePath);
}
return acc;
}, []);
// filePaths = [ '~/cool_app/foo.txt', '~/cool_app/.bar', '~/cool_app/baz.foo']
Filter and map an array3
// Only change code below this line
const filteredList = watchList
.filter(({ imdbRating }) => imdbRating >= 8.0)
.map(({ Title: title, imdbRating: rating }) => ({ title, rating }));
// Only change code above this line
console.log(filteredList);
Filter and map an array
const filteredList = watchList
.filter(movie => {
// return true it will keep the item
// return false it will reject the item
return parseFloat(movie.imdbRating) >= 8.0;
})
.map(movie => {
return {
title: movie.Title,
rating: movie.imdbRating
};
});