\$\begingroup\$
I need to optimize the piece of solution to filter out the object based on string values, maybe using some ES6 feature.
\$\endgroup\$
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)
3 Answers 3
\$\begingroup\$
\$\endgroup\$
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);
<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>
answered Jun 20, 2023 at 7:57
\$\begingroup\$
\$\endgroup\$
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:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/has
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
\$\begingroup\$
\$\endgroup\$
2
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)
-
2\$\begingroup\$ Why return !!str.match(usecaseObj.usecaseId) double exclamation? can't we use directly str.match(usecaseObj.usecaseId) \$\endgroup\$user274262– user2742622023年06月20日 07:52:25 +00:00Commented Jun 20, 2023 at 7:52
-
\$\begingroup\$ It is a double check !! converts the output to true or false \$\endgroup\$Danyal Ahmad– Danyal Ahmad2023年06月20日 07:57:45 +00:00Commented Jun 20, 2023 at 7:57
lang-js