如何获得基于搜索参数的新数据数组?

时间:2019-02-22 08:54:31

标签: javascript reactjs ecmascript-6 redux react-redux

我从API调用中请求了一些数据,这些数据需要过滤。

我正在使用Redux而不是本地状态进行操作。

这是我的动作:

import ActionTypes from '../constants/ActionTypes';


export const passengersDataAction = passengersData => ({
  type: ActionTypes.PASSENGERS_DATA,
  // This is the array of objects that I need to search through
  payload: { passengersData }, 
});

export const searchParamAction = searchParam => ({
  type: ActionTypes.SEARCH_PARAM,
  // This is the param that I need to send to passengersData
  // in order to get a new array of objects
  payload: { searchParam },
});

这是我的减速器

import createReducer from '../../../redux/createReducer';
import ActionTypes from '../constants/ActionTypes';

const initialState = {
  passengersData: [],
  searchParam: '',
};

const handlers = {
  [ActionTypes.PASSENGERS_DATA](state, action) {
    return {
      ...state,
      passengersData: action.payload.passengersData,
    };
  },

  [ActionTypes.SEARCH_PARAM](state, action) {
    return {
      ...state,
      searchParam: action.payload.searchParam,
    };
  },
};

export default createReducer(initialState, handlers);

上面是减速器。

我要基于searchParam的值返回一个新的对象数组。

类似这样的东西:

  [ActionTypes.SEARCH_PARAM](state, action) {
    return {
      ...state,
      passengersData://HERE GOES THE NEW ARRAY BASED ON WHAT searchParam RETURNS,
      searchParam: action.payload.searchParam,
    };
  },

在组件中,我正在这样做:

  <View>
    <TextInput
      style={PassengersStyles.SearchBox}
      // searchParamActionHandler sends the input value to the update the reducer
      onChangeText={text => searchParamActionHandler(text)}
      value={searchParam}
      placeholder="Search..."
    />
  </View>
  <Text>{searchParam}</Text>
  <PassengerCardBasedOnRoute searchParam={searchParam} />

PassengerCardBasedOnRoute是呈现passengersData中的对象数组的组件。 像这样:

           {passengersData.map(info => (
            <PassengersInfo
              key={info.id}
              searchParam={this.props.searchParam}
              cardinalpoint={info.cardinalpoint}
              name={info.name}
              address={info.address}
              datetime={info.timestamp}
            />
           }

这是对象数组的外观:

[
 {
  "id": 3,
  "name": "Marcos Alonso",
  "address": "Sabana",
  "phone": "712321222",
  "pickup": 0,
  "cardinalpoint": "N",
  "latitude": "9.93683450",
  "longitude": "-84.10991830",
  "timestamp": "2019-02-19 21:23:46",
  "dropofftimestamp": null,
  "pickuptimestamp": null,
  "deleted": null,
  "driver": 1
 },
 ...
]

那么,如何返回该新数组以呈现搜索条件?

2 个答案:

答案 0 :(得分:0)

已更新

如果我很了解您的需求,那么您应该这样做:我做了一点更改以仅映射按名称|地址过滤的数据

{
    passengersData.filter(item => item['name'] === this.props.searchParam || item['address'] === this.props.searchParam).map(info => (
        <PassengersInfo
          key={info.id}
          searchParam={this.props.searchParam}
          cardinalpoint={info.cardinalpoint}
          name={info.name}
          address={info.address}
          datetime={info.timestamp}
        />
 }

如果您要使用redux进行操作,则无需更改状态,但是您可以添加新状态(例如,passengersDataFiltred)并进行处理:

...
const initialState = {
   passengersData: [],
   passengersDataFiltred: [],
   searchParam: '',
};
...
[ActionTypes.SEARCH_PARAM](state, action) {
    const filtrededData = state.passengersData.filter(item => item['name'] === action.payload.searchParam || item['address'] === action.payload.searchParam);
    return {
        ...state,
        passengersDataFiltred: filtrededData,
        searchParam: action.payload.searchParam,
    };
}
...

然后在地图上映射passengerDataFiltred:

      {passengersDataFiltred.map(info => (
        <PassengersInfo
          key={info.id}
          searchParam={this.props.searchParam}
          cardinalpoint={info.cardinalpoint}
          name={info.name}
          address={info.address}
          datetime={info.timestamp}
        />
       }

如果这不是您想要的,请给我您的反馈?

答案 1 :(得分:0)

在“ SEARCH_PARAM”操作类型的化简中,您可以根据状态创建“ passengersData”的副本,并根据需要对该副本进行变异。

const initialState = {
  passengersData: [],
  searchParam: '',
  searchedData: []
};

[ActionTypes.SEARCH_PARAM](state, action) {
//In filter you can add your own logic to get the data
const searchedData = state.passengersData.filter((passenger) => passenger.name === action.payload.searchParam);

   return {
     ...state,
     searchedData,
     searchParam: action.payload.searchParam,
   };
},

仅供参考,这会将'passesngerData'替换为'searchedData'。如果您想保留原始的“ passengerData”,则可以在redux存储中创建一个新状态,然后从化简器中返回它。