javascript ES6过滤减少

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript ES6过滤减少相关的知识,希望对你有一定的参考价值。

/**
 * Filter an array with reduce which has a better performance.
 * @param {string} query - The string we want to search.
 * @param {array} list - The array we want to filter.
 * @param {string} prop - The property we want to match.
 * @returns {array} - Filtered list
 */
const searchReducer = (query, list, prop) =>
  list.reduce((prev, curr) => {
    if (curr[prop] === query) {
      prev.push(curr);
    }
    return prev;
  }, []);
  
// arrayList = [{ name: 'Foo' }, { name: 'Bar' }];
// searchReducer('Foo', arrayList, 'name'); 

以上是关于javascript ES6过滤减少的主要内容,如果未能解决你的问题,请参考以下文章