1

I have key identifiers that look like this...

const category = key; // could be "bus","car","plane"

for this example lets go with "bus". I also have an array of strings that look like this...

let transportationFields = [
      'bus_station', 'bus_stop', 'bus_line', 'took_bus'
      'car', 'buyer_of_car', 'car_model',
      'train_number', 'train_trip_num', 'train_stop',
    ];

Right now I'm grabbing the index of

const transFieldValues = transportationFields.indexOf(category) > -1; 

But that just returns true. How can I also grab the field it matched to? For example. Since category = bus. How can I return all the values it threw true to? ('bus_station', 'bus_stop', 'bus_line', 'took_bus') ?

Thank you for your help in advance!

2 Answers 2

4

You can use .filter() for finding all matches and return them in an array:

let transportationFields = [
  'bus_station', 'bus_stop', 'bus_line', 'took_bus',
  'car', 'buyer_of_car', 'car_model',
  'train_number', 'train_trip_num', 'train_stop',
];

let category = 'bus';

let transFieldValues = transportationFields.filter(function (item) {
  return item.indexOf(category) > -1;
});

document.write (JSON.stringify(transFieldValues));

Or with ES6 arrow function:

let transportationFields = [
  'bus_station', 'bus_stop', 'bus_line', 'took_bus',
  'car', 'buyer_of_car', 'car_model',
  'train_number', 'train_trip_num', 'train_stop',
];

let category = 'bus';

let transFieldValues = transportationFields.filter(w => w.indexOf(category) > -1);

document.write (JSON.stringify(transFieldValues));

If you don't mind leaving Edge users out of luck, then you can use .includes():

let transFieldValues = transportationFields.filter(w => w.includes(category));

... which just looks nicer than this > -1 you have to use with .indexOf().

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

3 Comments

I think you've got the right answer, i'm just trying to reproduce the syntax is react
transportationFields.filter(text => text.includes(category))
The Edge browser does not support includes(). But who cares ;-)
2

You can use Array#filter

let transportationFields = [
  'bus_station', 'bus_stop', 'bus_line', 'took_bus',
  'car', 'buyer_of_car', 'car_model',
  'train_number', 'train_trip_num', 'train_stop',
];

transportationFields = transportationFields.filter(e => {
  return e.indexOf('bus') != -1;
});

console.log(transportationFields)

You can also create a function and pass value to search

let transportationFields = ['bus_station', 'bus_stop', 'bus_line', 'took_bus','car', 'buyer_of_car', 'car_model','train_number', 'train_trip_num', 'train_stop'];

var findField = function(input) {
  return transportationFields.filter(e => {return e.indexOf(input) != -1;});
}

console.log(findField('bus'));

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.