1

I am trying to search for a value in an array and return the found keys with values.

Example:

var arr=['Volvo460', 'Fiat500', 'fiat400', 'volvo C7', 'Saab 95'];
var search = 'volvo';

expected result:

returned_array = ['Volvo460','volvo C7'];

Anyone?

Edit - conformed array ;-)

4
  • not a valid array format in JS Commented May 3, 2016 at 17:52
  • 1
    1=> ?? what is this? Commented May 3, 2016 at 17:52
  • doesn't this simply boil down to a linear search, else use JS objects. Commented May 3, 2016 at 17:54
  • Perhaps it is more easy to return the id's ? Commented May 3, 2016 at 17:56

6 Answers 6

3

Hope this answers:

var arr=['Volvo460', 'Fiat500', 'fiat400', 'volvo C7', 'Saab 95'];
var search = 'volvo';

var returned_array =arr.filter(function(itm,i,a){
  if(itm.toLowerCase().indexOf(search) > -1)
    return i==a.indexOf(itm);
});

alert(returned_array);

Here is an working example.

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

Comments

2

You can use filter() for filtering values from an array

var arr = [
  'Volvo460',
  'Fiat500',
  'fiat400',
  'volvo C7',
  'Saab 95'
];
var search = 'volvo';

var res = arr.filter(function(v) {
  // convert both string to lower to make it case insensitive
  return v.toLowerCase().indexOf(search.toLowerCase()) > -1;
});

document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>')

2 Comments

Don't you have you're parameters mixed up in your forEach call?
What if it just is an as described? (eg. not objectified..)
1

var arr = [
   'Volvo460',
   'Fiat500',
   'fiat400',
   'volvo C7',
   'Saab 95'
];
var search = 'volvo',

arr.map(function(v) {
  if(v.toLowerCase().indexOf(search.toLowerCase()) > -1) return v;
});

1 Comment

Thanks. Seems though that the returned array is filled with empty "spaces" alert(result) -> customer_id,customer_name,customer_finance_id,,,, Notice the four commas..
1

try this:

var obj = {
  1: 'Volvo460',
  2: 'Fiat500',
  3: 'Fiat400',
  4: 'volvo C7',
  5: 'Saab 95'
};
var search = 'volvo'
for (key in obj) {  
    if (obj[key].toLowerCase().search(search) == 0)
        console.log('found:' + key + '=' + obj[key])
}

the keys can be strings also:

var obj = {
  'a': 'Volvo460',
  'b': 'Fiat500',
  'c': 'Fiat400',
  'd': 'volvo C7',
  'e': 'Saab 95'
};
var search = 'volvo'
for (key in obj) {  
    if (obj[key].toLowerCase().search(search) == 0)
        console.log('found:' + key + '=' + obj[key])
}

if you need to add one key at a time you can do like this:

var obj = {};
obj['a'] = 'Volvo460';
obj['b'] = 'Fiat500',
obj['c'] = 'Fiat400',
obj['d'] = 'volvo C7',
obj['e'] = 'Saab 95'

var search = 'volvo'
for (key in obj){   
    if (obj[key].toLowerCase().search(search) == 0)
        console.log('found:' + key + '=' + obj[key])
}

Working example here: https://jsfiddle.net/Lfpk6zgo/5/

Comments

1

A proposal with a regular expression

var arr = ['Volvo460', 'Fiat500', 'fiat400', 'volvo C7', 'Saab 95'];
    search = RegExp('volvo', 'i'),
    result = arr.filter(search.test.bind(search));

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

Comments

1
var arr = ["volvoCar", "123volvo", "BMW", "VW", "etc"],
new_array = [];

for(var i = 0;i<arr.length;i++){
    if(arr[i].match(/volvo/ig)){
      new_array.push(arr[i]);
   }

}
console.log(new_array);

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.