2

I have a large array, and I want to filter it based on a string I pass it. This string could be one or two characters, up to an entire string.

Here's an example array:

var data = ["bourbon", "beer", "electric"]

If I pass e, I want the result ["electric", "beer"].

How can I achieve this functionality?

3
  • 1
    What code have you tried that isn't working? What is the beer object that you're referencing? Commented May 31, 2019 at 7:21
  • I think he wanted to write 'beer', 'electric'. Commented May 31, 2019 at 7:22
  • 2
    I can barely understand a single sentence. Please edit the question so it would be more understandable and clear what you are asking about. Commented May 31, 2019 at 7:24

5 Answers 5

3

Use filter with includes:

var data = ["bourbon", "beer", "electric"];
const selectByLetter = l => data.filter(s => s.includes(l));
console.log(selectByLetter("e"));
.as-console-wrapper { max-height: 100% !important; top: auto; }

ES5 syntax:

var data = ["bourbon", "beer", "electric"];
var selectByLetter = function(l) {
  return data.filter(function(s) {
    return s.indexOf(l) > -1;
  });
}
console.log(selectByLetter("e"));
.as-console-wrapper { max-height: 100% !important; top: auto; }

Sign up to request clarification or add additional context in comments.

5 Comments

@ jack Bashford I tried to run this code its showing beer ,electric but I want electric ,beer how to achieve it
So, you want to sort it in reverse alphabetical order?
I want this type of output .can you please refer this link w3schools.com/howto/howto_js_filter_lists.asp
But how is it sorted? Currently it's in the order provided in the array - should the output be reversed?
no I am asking same search bar functionality sorted the array .
0

you can use underscore.

var data = ["bourbon","beer", "electric"]
var filtered = [];
var filter = function(value) {
  filtered = _.filter(data, function(el) {
    return el.indexOf(value)
  })
  return filtered;
}

console.log('filtered',filter('e'));

Comments

0
var data = ["bourbon","beer", "electric"]
var result = data.filter(c => /e/i.test(c))
console.log('data',result);

Comments

0

For an O(N) solution instead of O(N^2), use a Set instead (Set.has is O(1), whereas String.prototype.includes is O(N)):

const data = ["bourbon", "beer", "electric"];
const setByData = data.reduce((a, str) => {
  a[str] = new Set(str);
  return a;
}, {});
console.log(
  Object.entries(setByData)
    .filter(([, set]) => set.has('e'))
    .map(([str]) => str)
);

3 Comments

@ CertainPerformance once I pass the string method I want this type of output.can you please refer this w3schools.com/howto/tryit.asp?filename=tryhow_js_filter_list
What do you mean? What exactly is the object structure you're looking for, can you describe it?
array['beer','bourbon,'electric'']. pass string "e" first its check first occurrence then second occurrence .['electric','beer'] i want this type of output.how to achieve it
0
products_name = ['ebay','eaby','ebb','ebbe','eaab','eeb']  
search_options = []  
const searched_word = "eb";  
for (const key in products_name)   
{  
    const search_count = products_name[key].search(new RegExp(searched_word, "i"));    
    if (search_count != -1)   
    {  
        search_options.push(products_name[key])  
    }  
}
console.log(search_options)  

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.