0

In the following scenario, how can I search both label and value?

var countries = [
    { label: 'United Kingdom', value: '44', group: "Europe" },
    { label: 'United States', value: '1', group: "North America" },
    { label: 'Norway', value: '47', group: "Europe" },
    { label: 'Sweden', value: '46', group: "Europe" },
    { label: 'Germany', value: '49', group: "Europe" }
];

text = 'uni';// Works fine since 'uni' is present in label
text = '44' // Should lookup value as well and return 'United Kingdom'

var suggestions = countries.filter(n => n.label.toLowerCase().includes(text));

console.log(suggestions);

Link to JS Bin

2
  • n => n.label.toLowerCase().includes(text) || n.value == text? Commented Apr 10, 2020 at 6:12
  • Change line to - var suggestions = countries.filter(n => (n.label.toLowerCase().includes(text) + n.value.toLowerCase().includes(text))); Commented Apr 10, 2020 at 6:26

7 Answers 7

2

just put or statement to filter

var suggestions = countries.filter(n => n.label.toLowerCase().includes(text) || n.value.toLowerCase().includes(text));
Sign up to request clarification or add additional context in comments.

Comments

1

You can implement a fuzzy search using all the values. Convert all value as word and then match.

Useful methods: Object.values, filter

const fuzzySearch = (countries, txt) => {
  return countries.filter((country) => {
    const word = Object.values(country).join("");
    return word.toLowerCase().includes(txt);
  });
};

const countries = [
  { label: "United Kingdom", value: "44", group: "Europe" },
  { label: "United States", value: "1", group: "North America" },
  { label: "Norway", value: "47", group: "Europe" },
  { label: "Sweden", value: "46", group: "Europe" },
  { label: "Germany", value: "49", group: "Europe" },
];
const suggestions = (keyword) => fuzzySearch(countries, keyword);
console.log(suggestions("uni"));
console.log(suggestions("44"));

Comments

0

Just go for the and(&&) operator, I would also suggest making the 'suggestions' as a method which takes two parameters and fetches the correct item.

num = '46';
text = 'Sweden';
var suggestions = countries.filter(n => RegExp(text,'i').test(n.label) && n.value == num);

Comments

0

Try this

var countries = [
    { label: 'United Kingdom', value: '44', group: "Europe" },
    { label: 'United States', value: '1', group: "North America" },
    { label: 'Norway', value: '47', group: "Europe" },
    { label: 'Sweden', value: '46', group: "Europe" },
    { label: 'Germany', value: '49', group: "Europe" }
];

text = '44';

var suggestions = countries.filter(n => {  
  if(n.label.toLowerCase().includes(text)) {
    return true;
  }

  if (n.value.includes(text)) {
    return true;
  }

  return false;
});

console.log(suggestions);

Comments

0

Just use OR condition, Following code will work for label,value,group

var countries = [
    { label: 'United Kingdom', value: '44', group: "Europe" },
    { label: 'United States', value: '1', group: "North America" },
    { label: 'Norway', value: '47', group: "Europe" },
    { label: 'Sweden', value: '46', group: "Europe" },
    { label: 'Germany', value: '49', group: "Europe" }
];

text = 'europe';// Works
text = 'uni' // works
text = 44 // works

var suggestions = countries.filter(n => {

  return ( 
        n.label.toLowerCase().includes(text) ||
        n.group.toLowerCase().includes(text) ||
        n.value.includes(text)
     )

});

console.log(suggestions);

Comments

0
text = '44';
label = 'United Kingdom'

const suggestions = countries.filter(n => n.label.toLowerCase() === label.toLowerCase() && n.value === text);

I think what i understand, is that you just need to set && or || statement and set condition for the label too.

Comments

0

You can use Object.values and some and includes

var countries = [{ label: 'United Kingdom', value: '44', group: "Europe" },{ label: 'United States', value: '1', group: "North America" },{ label: 'Norway', value: '47', group: "Europe" },{ label: 'Sweden', value: '46', group: "Europe" },{ label: 'Germany', value: '49', group: "Europe" }];

const suggestions = searchTerm => countries.filter(n => Object.values(n).some(value => value.toLowerCase().includes(searchTerm)));

console.log(suggestions("uni"));
console.log(suggestions("44"));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.