leila leila - 4 months ago 14
Javascript Question

in javascript,how do we search array by partial value of property

is anyone can help to Complete the functions below,

should run this function to return a filtered list of iphones

function filterBySearchTerm(iphones, searchTerm) {
return iphones.filter(doFilter)
}


Complete this function to use the 'searchTerm' argument to return an iphone that matches the 'searchTerm'
You should match the
color
and
capacity
fields.
e.g. if I use the search term 'Gold' this should return true for iphones that have a color value of 'Rose Gold' and 'Gold', if I use the search term '64' this should return true for iphones where the capacity is value of '64GB'

function doFilter(iphone, searchTerm) {
return () => iphone;
}
}

const iphones = [
{
"id": 1,
"name": "iPhone SE",
"color": "Silver",
"type": "Wifi",
"capacity": "16GB",
"price": 629
},
{
"id": 2,
"name": "iPhone SE",
"color": "Silver",
"type": "Wifi",
"capacity": "64GB",
"price": 829
},
{
"id": 3,
"name": "iPhone SE",
"color": "Gold",
"type": "Wifi",
"capacity": "16GB",
"price": 629
},
{
"id": 4,
"name": "iPhone SE",
"color": "Gold",
"type": "Wifi",
"capacity": "64GB",
"price": 829
},
{
"id": 5,
"name": "iPhone SE",
"color": "Space Grey",
"type": "Wifi",
"capacity": "16GB",
"price": 629
},
{
"id": 6,
"name": "iPhone SE",
"color": "Space Grey",
"type": "Wifi",
"capacity": "64GB",
"price": 829
},
{
"id": 7,
"name": "iPhone SE",
"color": "Rose Gold",
"type": "Wifi",
"capacity": "16GB",
"price": 629
},
{
"id": 8,
"name": "iPhone SE",
"color": "Rose Gold",
"type": "Wifi",
"capacity": "64GB",
"price": 829
}
];

Answer

This is easily implemented with Array.filter() and a simple regular expression.

function filterBySearchTerm(iphones, searchTerm) {
  return iphones.filter(item => {
    let re = new RegExp(searchTerm, 'i'); //ignore case
    return item.capacity.match(re) || item.color.match(re);
  });
}

For a complete example, expand the sample below:

function filterBySearchTerm(iphones, searchTerm) {
  return iphones.filter(item => {
    let re = new RegExp(searchTerm, 'i'); //ignore case
    return item.capacity.match(re) || item.color.match(re);
  });
}


const iphones = [{
  "id": 1,
  "name": "iPhone SE",
  "color": "Silver",
  "type": "Wifi",
  "capacity": "16GB",
  "price": 629
}, {
  "id": 2,
  "name": "iPhone SE",
  "color": "Silver",
  "type": "Wifi",
  "capacity": "64GB",
  "price": 829
}, {
  "id": 3,
  "name": "iPhone SE",
  "color": "Gold",
  "type": "Wifi",
  "capacity": "16GB",
  "price": 629
}, {
  "id": 4,
  "name": "iPhone SE",
  "color": "Gold",
  "type": "Wifi",
  "capacity": "64GB",
  "price": 829
}, {
  "id": 5,
  "name": "iPhone SE",
  "color": "Space Grey",
  "type": "Wifi",
  "capacity": "16GB",
  "price": 629
}, {
  "id": 6,
  "name": "iPhone SE",
  "color": "Space Grey",
  "type": "Wifi",
  "capacity": "64GB",
  "price": 829
}, {
  "id": 7,
  "name": "iPhone SE",
  "color": "Rose Gold",
  "type": "Wifi",
  "capacity": "16GB",
  "price": 629
}, {
  "id": 8,
  "name": "iPhone SE",
  "color": "Rose Gold",
  "type": "Wifi",
  "capacity": "64GB",
  "price": 829
}];


console.log(filterBySearchTerm(iphones, "64"));
console.log(filterBySearchTerm(iphones, "gold"));