Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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>
Comment

map & filter

const filteredList = watchList.map(movie => ({
  title : movie['Title'],
  rating: movie["imdbRating"]
})
).filter(movie => {
  return parseFloat(movie.rating) >= 8.0;
})
Comment

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']
Comment

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']
Comment

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);
Comment

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
    };
  });
Comment

PREVIOUS NEXT
Code Example
Javascript :: read and save excel with react 
Javascript :: render html page in javascript 
Javascript :: how to compile typescript to javascript es6 
Javascript :: React closing a dropdown when click outside 
Javascript :: javascript stack 
Javascript :: how to change textContent in js 
Javascript :: node.js global variables 
Javascript :: index.js:1 You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors. 
Javascript :: js opposite of includes 
Javascript :: Change Title In React Project 
Javascript :: javascript regular expression end of string 
Javascript :: check property exists 
Javascript :: split function in javascript 
Javascript :: node.js error handling 
Javascript :: connect redux 
Javascript :: puppeteer jquery 
Javascript :: setup error handler in express framework 
Javascript :: Find Largest Number by function by javascript 
Javascript :: vue 
Javascript :: angular flex layout 
Javascript :: how to change port in next js 
Javascript :: how to display a calender in react native 
Javascript :: jquery plugins 
Javascript :: remove two things from javascript string 
Javascript :: empty array 
Javascript :: AJAX in reload a div container 
Javascript :: rating 
Javascript :: Force users to update your application in React Native 
Javascript :: javascript prototype inheritance 
Javascript :: open source code 
ADD CONTENT
Topic
Content
Source link
Name
3+1 =