I am trying to search a list of objects using a list of values and a list of keys. My first approach is to create a list of objects given a particular value. However, the key is hard-coded and I should be using a more functional approach. Next, I think I should create a list of objects after filtering for each value. However, I am again hard-coding the values. I am not sure how to pass a list of values and a list of keys such as the following. Should I have used a Map object? if so, how can I extract the following two variables?:
const list_of_keys = ['color_1', 'color_2', 'color_3']
const list_of_values = ['red','blue','purple']`
const data = [
{make: 'ford',model: 'mustang',color_1: 'red',color_2: '',color_3: ''},
{make: 'ford',model: 'escape',color_1: '',color_2: 'blue',color_3: ''},
{make: 'ford',model: 'expedition',color_1: '',color_2: '',color_3: 'purple'},
{make: 'mercedez',model: 'helicopter',color_1: '',color_2: '',color_3: 'orange'}
]
// hard-coded object keys
const filter_by_multiple_keys = (carObject, Value) => carObject.filter(car =>
car.color_1 === Value ||
car.color_2 === Value ||
car.color_3 === Value
);
// hard-coded values
const filterByColorsObject = list_of_objects => {
const dataArray = [];
dataArray.push(filter_by_multiple_keys(list_of_objects, 'red'));
dataArray.push(filter_by_multiple_keys(list_of_objects, 'blue'));
dataArray.push(filter_by_multiple_keys(list_of_objects, 'purple'));
return(dataArray)
}
console.log(filterByColorsObject(data))