skip to Main Content
const usecases =  [
    { usecaseId: 'Bill Gates', id: 1, description: 'CEO & founder, Microsoft' },
    { usecaseId: 'Mark Zuckerberg', id: 2, description: 'CEO, Facebook' },
    { usecaseId: 'Steve Jobs', id: 3, description: 'CEO & co-founder, Apple' },
    { usecaseId: 'Satya Nadella', id: 4, description: 'CEO, Microsoft' },
    { usecaseId: 'Elon Musk', id: 5, description: 'CEO, Tesla & SpaceX' },
    { usecaseId: 'Ginni Rometty', id: 6, description: 'CEO, IBM' },
    { usecaseId: 'Jeff Bezos', id: 7, description: 'CEO, Amazon' },
    { usecaseId: 'Larry Page', id: 8, description: 'CEO, Google' },
    { usecaseId: 'Jack Dorsey', id: 9, description: 'CEO, Twitter' },
    { usecaseId: 'Meg Whitman', id: 10, description: 'CEO, HP' },
    { usecaseId: 'Tim Cook', id: 11, description: 'CEO, Apple' },
    { usecaseId: 'Jeff Weiner', id: 12, description: 'CEO, LinkedIn' },
  ]
  
 const str = "Bill Gates, Mark Zuckerberg, Tim Cook, Jeff Bezos, Steve Jobs";
 
 const newArr = usecases.filter(usecaseObj => {
    if(str.indexOf(usecaseObj.usecaseId) != -1) {
      return usecaseObj
    }
 });
 console.log(newArr)

I need to optimize the piece of solution to filter out the object based on string values, maybe using some ES6 feature.

2

Answers


  1. const usecases =  [
        { usecaseId: 'Bill Gates', id: 1, description: 'CEO & founder, Microsoft' },
        { usecaseId: 'Mark Zuckerberg', id: 2, description: 'CEO, Facebook' },
        { usecaseId: 'Steve Jobs', id: 3, description: 'CEO & co-founder, Apple' },
        { usecaseId: 'Satya Nadella', id: 4, description: 'CEO, Microsoft' },
        { usecaseId: 'Elon Musk', id: 5, description: 'CEO, Tesla & SpaceX' },
        { usecaseId: 'Ginni Rometty', id: 6, description: 'CEO, IBM' },
        { usecaseId: 'Jeff Bezos', id: 7, description: 'CEO, Amazon' },
        { usecaseId: 'Larry Page', id: 8, description: 'CEO, Google' },
        { usecaseId: 'Jack Dorsey', id: 9, description: 'CEO, Twitter' },
        { usecaseId: 'Meg Whitman', id: 10, description: 'CEO, HP' },
        { usecaseId: 'Tim Cook', id: 11, description: 'CEO, Apple' },
        { usecaseId: 'Jeff Weiner', id: 12, description: 'CEO, LinkedIn' },
      ]
      
     const str = "Bill Gates, Mark Zuckerberg, Tim Cook, Jeff Bezos, Steve Jobs";
    
    const newArr = usecases.filter(usecaseObj => {
      return !!str.split(", ").find(el => el == usecaseObj.usecaseId)
    })
    
    console.log(newArr)
    Login or Signup to reply.
  2. You could first create an array of persons and then use find to get them out of the usecases:

    const usecases =  [
        { usecaseId: 'Bill Gates', id: 1, description: 'CEO & founder, Microsoft' },
        { usecaseId: 'Mark Zuckerberg', id: 2, description: 'CEO, Facebook' },
        { usecaseId: 'Steve Jobs', id: 3, description: 'CEO & co-founder, Apple' },
        { usecaseId: 'Satya Nadella', id: 4, description: 'CEO, Microsoft' },
        { usecaseId: 'Elon Musk', id: 5, description: 'CEO, Tesla & SpaceX' },
        { usecaseId: 'Ginni Rometty', id: 6, description: 'CEO, IBM' },
        { usecaseId: 'Jeff Bezos', id: 7, description: 'CEO, Amazon' },
        { usecaseId: 'Larry Page', id: 8, description: 'CEO, Google' },
        { usecaseId: 'Jack Dorsey', id: 9, description: 'CEO, Twitter' },
        { usecaseId: 'Meg Whitman', id: 10, description: 'CEO, HP' },
        { usecaseId: 'Tim Cook', id: 11, description: 'CEO, Apple' },
        { usecaseId: 'Jeff Weiner', id: 12, description: 'CEO, LinkedIn' },
      ]
      
     const str = "Bill Gates, Mark Zuckerberg, Tim Cook, Jeff Bezos, Steve Jobs";
     
     let result = []
     
     const persons = str.split(',')
     
     persons.forEach((person) => {
        result.push(usecases.find(uc => uc.usecaseId === person.trim()))
     })
     
     console.log(result)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search