简体   繁体   中英

Javascript | Filtering in nested arrays

I need to filter the following data, with the text that the user types inside an input, but only in 3 keys (date, pizza and size)

  var pizzaData = [{
    "id": 1,
    "date": "2020-03-20",
    "order": [
      {"pizza": "chicken", "comment": "No onion", "size": "personal"}, 
      {"pizza": "cheese", "comment": "", "size": "medium"}
    ]}, {
    "id": 2,
    "date": "2020-03-21",
    "order": [
      {"pizza": "veggie", "comment": "Extra cheese", "size": "big"}
    ]}, {
    "id": 3,
    "date": "2020-03-22",
    "order": [
      {"pizza": "Pepperoni", "comment": "", "size": "big"},
      {"pizza": "Double cheese", "comment": "", "size": "big"},
      {"pizza": "BBQ chicken", "comment": "", "size": "small"},
      {"pizza": "Mushrooms", "comment": "", "size": "big"}
    ]}, {
    "id": 4,
    "date": "2020-03-25",
    "order": [
      {"pizza": "cheese", "comment": "", "size": "small"}
    ]}
  ]

I'm using angularjs, I've tried using $filter, creating a filter and also filter method in js, but I can only get the first object inside "order" array. The code should work in Chrome, Mozilla and Explorer 11

I'm trying to achieve something like this

input = "cheese"

pizzaData = [{
    "id": 1,
    "date": "2020-03-20",
    "order": [
      {"pizza": "chicken", "comment": "No onion", "size": "personal"}, 
      {"pizza": "cheese", "comment": "", "size": "medium"}
    ]}, {
    "id": 4,
    "date": "2020-03-25",
    "order": [
      {"pizza": "cheese", "comment": "", "size": "small"}
    ]}
  ]




input = 2020-03-21

pizzaData = [{
    "id": 2,
    "date": "2020-03-21",
    "order": [
      {"pizza": "veggie", "comment": "Extra cheese", "size": "big"}
    ]}
  ]



input = onion

pizzaData = []


You can create a string of all the values that you want to search in, in each order and check for the searched key in that string.

 var pizzaData = [{ "id": 1, "date": "2020-03-20", "order": [ {"pizza": "chicken", "comment": "No onion", "size": "personal"}, {"pizza": "cheese", "comment": "", "size": "medium"} ]}, { "id": 2, "date": "2020-03-21", "order": [ {"pizza": "veggie", "comment": "Extra cheese", "size": "big"} ]}, { "id": 3, "date": "2020-03-22", "order": [ {"pizza": "Pepperoni", "comment": "", "size": "big"}, {"pizza": "Double cheese", "comment": "", "size": "big"}, {"pizza": "BBQ chicken", "comment": "", "size": "small"}, {"pizza": "Mushrooms", "comment": "", "size": "big"} ]}, { "id": 4, "date": "2020-03-25", "order": [ {"pizza": "cheese", "comment": "", "size": "small"} ]} ] function getOrder(key) { return pizzaData.filter((eachOrder) => { let text = [eachOrder.date]; text = text.concat(eachOrder.order.map(({pizza, size}) => ([pizza, size]))); return text.join(' ').includes(key); }) } console.log('2020-03-21 => ', getOrder('2020-03-21')) console.log("cheese => ", getOrder("cheese"))

 var pizzaData = [{ "id": 1, "date": "2020-03-20", "order": [ {"pizza": "chicken", "comment": "No onion", "size": "personal"}, {"pizza": "cheese", "comment": "", "size": "medium"} ]}, { "id": 2, "date": "2020-03-21", "order": [ {"pizza": "veggie", "comment": "Extra cheese", "size": "big"} ]}, { "id": 3, "date": "2020-03-22", "order": [ {"pizza": "Pepperoni", "comment": "", "size": "big"}, {"pizza": "Double cheese", "comment": "", "size": "big"}, {"pizza": "BBQ chicken", "comment": "", "size": "small"}, {"pizza": "Mushrooms", "comment": "", "size": "big"} ]}, { "id": 4, "date": "2020-03-25", "order": [ {"pizza": "cheese", "comment": "", "size": "small"} ]} ] function search(data){ let searchData=[]; if(data.split("-").length==3){ for(var i=0;i<pizzaData.length;i++){ if(pizzaData[i].date==data){ searchData.push(pizzaData[i]); } } return searchData; } for(var i=0;i<pizzaData.length;i++){ let orders=pizzaData[i].order; for(var j=0;j<orders.length;j++){ if(orders[j].pizza.toLowerCase().match(data.toLowerCase()) || orders[j].size.toLowerCase().match(data.toLowerCase())){ searchData.push(pizzaData[i]); } } } return searchData; } console.log(search("2020-03-21")); console.log(search("cheese"));

var pizzaData = [{
"id": 1,
"date": "2020-03-20",
"order": [
  {"pizza": "chicken", "comment": "No onion", "size": "personal"}, 
  {"pizza": "cheese", "comment": "", "size": "medium"}
]}, {
"id": 2,
"date": "2020-03-21",
"order": [
  {"pizza": "veggie", "comment": "Extra cheese", "size": "big"}
]}, {
"id": 3,
"date": "2020-03-22",
"order": [
  {"pizza": "Pepperoni", "comment": "", "size": "big"},
  {"pizza": "Double cheese", "comment": "", "size": "big"},
  {"pizza": "BBQ chicken", "comment": "", "size": "small"},
  {"pizza": "Mushrooms", "comment": "", "size": "big"}
]}, {
"id": 4,
"date": "2020-03-25",
"order": [
  {"pizza": "cheese", "comment": "", "size": "small"}
]} ]

 const searchData = keywords => {
  const filteredData = pizzaData.filter(pizza => {

const filteredOrder = pizza.order.filter(({pizza, size}) => {
  return (pizza === keywords || size === keywords)
})

return (pizza.date === keywords || !! 
  (filteredOrder.length))

   })
    return filteredData
 }

  console.log(searchData('small'))

Above answers are great, I'm sharing my try. I'm using filter function and inside that using some to detect any match case(on order):

 var pizzaData = [{ "id": 1, "date": "2020-03-20", "order": [ {"pizza": "chicken", "comment": "No onion", "size": "personal"}, {"pizza": "cheese", "comment": "", "size": "medium"} ]}, { "id": 2, "date": "2020-03-21", "order": [ {"pizza": "veggie", "comment": "Extra cheese", "size": "big"} ]}, { "id": 3, "date": "2020-03-22", "order": [ {"pizza": "Pepperoni", "comment": "", "size": "big"}, {"pizza": "Double cheese", "comment": "", "size": "big"}, {"pizza": "BBQ chicken", "comment": "", "size": "small"}, {"pizza": "Mushrooms", "comment": "", "size": "big"} ]}, { "id": 4, "date": "2020-03-25", "order": [ {"pizza": "cheese", "comment": "", "size": "small"} ]} ]; let filterPizza=(filterData)=>{ result=pizzaData.filter((value)=> value.date == filterData || value.order.some((elem)=>elem.pizza.toUpperCase()==filterData.toUpperCase() || elem.size.toUpperCase()==filterData.toUpperCase())) return result; } console.log(filterPizza('cheese')); console.log(filterPizza('2020-03-21'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM