如何过滤对象数组并列出其中一个值中包含特定关键字的所有对象

How to filter through an array of objects and list all objects containing a certain keyword in one of it's values

我正在处理一组书籍对象,任务是 console.log 列出所有书名中包含“公司”或“人”的书籍。

const books = [{
    title: 'How to win friends and influence people',
    authors: ['Dale Carnegie'],
    publisher: 'Pocket Books',
    year: '1936'
  }, {
    title: 'Management: tasks, responsibilities, practices',
    authors: ['Peter F. Drucker'],
    publisher: 'Harper Business',
    year: '1985'
  }, {
    title: 'Strength finder 2.0',
    authors: ['Tom Rath'],
    publisher: 'Gallup Press',
    year: '2007'
  }, {
    title: 'In search of excellence: Lessons from America\'s best-run companies',
    authors: ['Thomas Peters', ' Robert H. Waterman'],
    publisher: 'Harper Collins',
    year: '1982'
  }, {
    title: 'Built to last: Successful habits of visionary companies',
    authors: ['James C. Collins', 'Jerry I. Porras'],
    publisher: 'Harper Collins',
    year: '1994'
  }, {
    title: 'Reengineering the corporation: A manifesto for business revolution',
    authors: ['Michael Hammer', 'James A. Champy'],
    publisher: 'Harper Collins',
    year: '1993'
  }, {
    title: 'Competitive advantage: Creating and sustaining superior performance',
    authors: ['Michael E. Porter'],
    publisher: 'Free Press',
    year: '1998'
  }, {
    title: 'Crossing the chasm: Marketing and selling technology products to mainstream customers',
    authors: ['Geoffrey A. Moore', 'Regis McKenna'],
    publisher: 'Pocket Books',
    year: '1936'
  }, {
    title: '7 habits of highly effective people: Powerful lessons in personal change',
    authors: ['Stephen R. Covey'],
    publisher: 'Simon and Shuster',
    year: '1990'
  }, {
    title: 'The Six Sigma Way',
    authors: ['Peter S. Pande et al', 'Robert P. Neuman', 'Roland R. Cavanagh'],
    publisher: 'McGraw Hill',
    year: '2000'
  }, {
    title: 'The innovator\'s dilemma: When new technologies cause great firms to fail',
    authors: ['Clayton M. Christensen'],
    publisher: 'Harvard Business School Press',
    year: '1997'
  }, {
    title: 'The Essential Drucker',
    authors: ['Peter F. Drucker'],
    publisher: 'Harper Business',
    year: '2001'
  }];

到目前为止,我已尝试使用 filter()includes(),但它只返回包含关键字“people”的书籍

let keyWord = books.filter (bookTitle => bookTitle.title.includes('people'||'companies'));
console.log(关键字);

我建议使用 Array.filter() along with Array.some() 来匹配所需的结果。

我们创建一个关键字数组,然后 return 匹配其中 some 个的任何项目。

const books = [{ title: 'How to win friends and influence people', authors: ['Dale Carnegie'], publisher: 'Pocket Books', year: '1936' }, { title: 'Management: tasks, responsibilities, practices', authors: ['Peter F. Drucker'], publisher: 'Harper Business', year: '1985' }, { title: 'Strength finder 2.0', authors: ['Tom Rath'], publisher: 'Gallup Press', year: '2007' }, { title: 'In search of excellence: Lessons from America\'s best-run companies', authors: ['Thomas Peters', ' Robert H. Waterman'], publisher: 'Harper Collins', year: '1982' }, { title: 'Built to last: Successful habits of visionary companies', authors: ['James C. Collins', 'Jerry I. Porras'], publisher: 'Harper Collins', year: '1994' }, { title: 'Reengineering the corporation: A manifesto for business revolution', authors: ['Michael Hammer', 'James A. Champy'], publisher: 'Harper Collins', year: '1993' }, { title: 'Competitive advantage: Creating and sustaining superior performance', authors: ['Michael E. Porter'], publisher: 'Free Press', year: '1998' }, { title: 'Crossing the chasm: Marketing and selling technology products to mainstream customers', authors: ['Geoffrey A. Moore', 'Regis McKenna'], publisher: 'Pocket Books', year: '1936' }, { title: '7 habits of highly effective people: Powerful lessons in personal change', authors: ['Stephen R. Covey'], publisher: 'Simon and Shuster', year: '1990' }, { title: 'The Six Sigma Way', authors: ['Peter S. Pande et al', 'Robert P. Neuman', 'Roland R. Cavanagh'], publisher: 'McGraw Hill', year: '2000' }, { title: 'The innovator\'s dilemma: When new technologies cause great firms to fail', authors: ['Clayton M. Christensen'], publisher: 'Harvard Business School Press', year: '1997' }, { title: 'The Essential Drucker', authors: ['Peter F. Drucker'], publisher: 'Harper Business', year: '2001' }];

const keyWords = ['people', 'companies'];

function findMatchingTitles(books, keyWords) { 
    return books.filter(({title}) => { 
        return keyWords.some(keyword => title.includes(keyword));
    })
}

console.log('Matching titles:');
const result = findMatchingTitles(books, ['people', 'companies']);
result.forEach(book => console.log(book.title));
.as-console-wrapper { max-height: 100% !important; top: 0; }

不能在 include() 中使用 OR 运算符。所以你可以把逻辑写在外面。

books.filter(book => book.title.includes('companies') || book.title.includes('people'))

如果匹配需要不区分大小写:

books.filter(book => {
    const titleLower = book.title.toLowerCase()
    return titleLower.includes('companies') || titleLower.includes('people')
  })

您可以使用正则表达式:

const peopleRegex = /people/i
const companiesRegex = /companies/i

const result = books.filter(item=>peopleRegex.test(item.title) || companiesRegex.test(item.title))