Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

javascript - Where did I make the mistake of changing my search filter? And how to fix it?

I have a list of airplanes departing or arriving at the airport and i also had a search filter where i enter the time of arrival or departure and filtering of the airplanes. I do this using the actual property of my API. But I needed to change my search. Now I need to search by flight number - the planeTypeID.code property of my API. But when I changed it, stopped showing a list of airplanes. What is my mistake and how to fix it?

I just instead actual everywhere wrote ["planeTypeID.code"] and delete method: .split("-").reverse().join("-")

OLD version:
small part airplane.js(reducer)

case "RUN_FILTER":
      var newData = state.data[action.shift || state.shift].filter(x => {
        return (
          x.actual &&               
          x.actual.includes(         
            state.day
              .split("-")           
              .reverse()             
              .join("-")               
          )
        );
      });

    case "LOAD_DATA_END":
      var newData = action.payload.data[state.shift].filter(x => {
        return (
          x.actual &&                      
          x.actual.includes(                   
            action.payload.day
              .split("-")                             
              .reverse()                
              .join("-")                 
          )
        );
      });


small part app.js(main component)

export function searchFilter(search, data) {
  return data.filter(n => n.actual.toLowerCase().includes(search)); 
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

NEW version:

small part airplane.js(reducer)

    case "RUN_FILTER":

      var newData = state.data[action.shift || state.shift].filter(x => {
        return (
          x["planeTypeID.code"] &&                   // Сhange 
          x["planeTypeID.code"].includes(            // Сhange 
            state.day
                                                      // delete                                
          )
        );
      });
      return Object.assign({}, state, {

    case "LOAD_DATA_END":
      var newData = action.payload.data[state.shift].filter(x => {
        return (
          x["planeTypeID.code"] &&                   // Сhange        
          x["planeTypeID.code"].includes(            // Сhange   
            action.payload.day
                                                     // delete                                    
          )
        );
      });

small part app.js(main component)


export function searchFilter(search, data) {
  return data.filter(n => n["planeTypeID.code"].toLowerCase().includes(search)); // Сhange   
}


All project code in sandbox:
https://codesandbox.io/s/redux-ant-design-filter-table-column-with-slider-jj6mu

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

An example value of x["planeTypeID.code"] is "B734", of state.day "23-08-2019" => those are 2 different fields => you will get an empty array when you filter by x["planeTypeID.code"].includes(state.day) ˉ\_(ツ)_/ˉ

After debugging via comments, the most likely solution is:

x["planeTypeID.code"].toLowerCase().includes(action.search || state.search)

I recommend to Get Started with Debugging JavaScript as a generic first step to questions that start with:

Where did I make the mistake...

...before posting to Stack Overflow.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...