0
\$\begingroup\$

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.

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

The fastest way is to use Set:

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 set = new Set(str.split(/,\s+/));

const result = usecases.filter(usecase => set.has(usecase.usecaseId));

console.log(result);

enter image description here

<script benchmark data-count="1">

    const usecases = Array.from({ length: 100000 }).reduce(arr => arr.push(...[
        { 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' },
    ]) && arr, []);

    const str = "Bill Gates, Mark Zuckerberg, Tim Cook, Jeff Bezos, Steve Jobs";

    // @benchmark Set

    const set = new Set(str.split(/,\s+/));

    usecases.filter(usecase => set.has(usecase.usecaseId));

    // @benchmark regex

    usecases.filter(usecaseObj => {
        return !!str.match(usecaseObj.usecaseId)
    });


</script>
<script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>

\$\endgroup\$
1
\$\begingroup\$

This is what I am seeing as an answer:

const data = [
  { id: 1, usecaseId: "Bill Gates", description: "CEO & founder, Microsoft" },
  { id: 2, usecaseId: "Mark Zuckerberg", description: "CEO, Facebook" },
  { id: 3, usecaseId: "Steve Jobs", description: "CEO & co-founder, Apple" },
  { id: 4, usecaseId: "Satya Nadella", description: "CEO, Microsoft" },
  { id: 5, usecaseId: "Elon Musk", description: "CEO, Tesla & SpaceX" },
  { id: 6, usecaseId: "Ginni Rometty", description: "CEO, IBM" },
  { id: 7, usecaseId: "Jeff Bezos", description: "CEO, Amazon" },
  { id: 8, usecaseId: "Larry Page", description: "CEO, Google" },
  { id: 9, usecaseId: "Jack Dorsey", description: "CEO, Twitter" },
  { id: 10, usecaseId: "Meg Whitman", description: "CEO, HP" },
  { id: 11, usecaseId: "Tim Cook", description: "CEO, Apple" },
  { id: 12, usecaseId: "Jeff Weiner", description: "CEO, LinkedIn" },
];

const str = "Bill Gates, Mark Zuckerberg, Tim Cook, Jeff Bezos, Steve Jobs";

const filterByKeywords = (data, keywords) => {
  const subStrings = new Set(keywords.split(", "));
  const result = data.filter((datum) => subStrings.has(datum.usecaseId));
  return result;
};

console.log(filterByKeywords(data, str));

Code above utilises ES6 function has() which is exclusive to Set(). This can be modified further by replacing Set() with Array() and .has() with .includes() if only you're confident that keywords cannot contain repeated values.

References:

\$\endgroup\$
0
\$\begingroup\$

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.match(usecaseObj.usecaseId)
})

console.log(newArr)

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Why return !!str.match(usecaseObj.usecaseId) double exclamation? can't we use directly str.match(usecaseObj.usecaseId) \$\endgroup\$
    – user274262
    Commented Jun 20, 2023 at 7:52
  • \$\begingroup\$ It is a double check !! converts the output to true or false \$\endgroup\$ Commented Jun 20, 2023 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.