Ответы пользователя по тегу React
  • Приложение на Next.js.Как реализовать поиск по объекту городов?

    Andy8888
    @Andy8888 Автор вопроса
    Frontend Developer
    Реализован в reduxToolkit

    searchCity(state, action){
          if (action.payload.length !== 0) {
            let result = Object.entries(state.cities).filter(el => {
              let isEmpty = Object.keys(el[1]).some(city => city.toLowerCase().includes(action.payload.toLowerCase().trimEnd()))
              if (isEmpty) {
                Object.values(el[1]).map(el => {
                  if (el.name.toLowerCase().includes(action.payload.toLowerCase().trimEnd())) {
                    el.isQueryItem = true
                  } else {
                    el.isQueryItem = false
                  }
                })
                return el
              }
            })
      
            state.filter_cities = result.reduce((newObj, item) => {
              newObj[item[0]] = item[1];
              return newObj;
            }, {});
      
          } else {
            let result = Object.entries(state.cities).filter(el => {
              Object.values(el[1]).map(el => {
                el.isQueryItem = false
              })
              return el
            })
      
            state.filter_cities = result.reduce((newObj, item) => {
              newObj[item[0]] = item[1];
              return newObj;
            }, {});
          }
        }
      },
    Ответ написан
    Комментировать