javascript - Map and filter an array at the same time - Stack Overflow

PHOTO EMBED

Thu Jul 14 2022 13:21:42 GMT+0000 (Coordinated Universal Time)

Saved by @arielvol #javascript

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

https://stackoverflow.com/questions/34398279/map-and-filter-an-array-at-the-same-time