1

i use this lib to show my array https://github.com/oblador/react-native-collapsible and get array from API and use Redux-Thunk . now i want to search in array this is my sample of data :

(3) [{…}, {…}, {…}]
0: {HARDWAREID: 420474797787, NICKNAME: "رضا نوری پور ایران 62 - 374 ع 66", SENTDATE: "14:47", XPOINT: 51.2906833, YPOINT: 35.6797716, …}
1: {HARDWAREID: 409792319815, NICKNAME: "ایران 78 - 875 ع 73", SENTDATE: "14:47", XPOINT: 55.35633, YPOINT: 29.964019, …}
2: {HARDWAREID: 2225434572, NICKNAME: "عابدین پور 938ع43", SENTDATE: "1398-06-19 17:54", XPOINT: 49.49388, YPOINT: 37.47155, …}

for search box i use this lib https://react-native-training.github.io/react-native-elements/

this is code of search box:

<SearchBar
                        placeholder="جستجو"
                        onChangeText={this.updateSearch}
                        value={search}
                        containerStyle={{ backgroundColor: '#fff', borderRadius: 13,borderWidth:0.4, padding: 5,margin:8,height:50,width:wp('95%'),textAlign:'right'}}
                        inputContainerStyle={{ backgroundColor: '#fff',}}
                    />

in 'onChange' use this code :

updateSearch = search => {
        this.setState({search:search})
       // console.log(search)
       var found = this.props.data.find(function(element) {
        return element.POSDESCRIPTION.i === search;
     });
     console.log(found)
    };

but in console.log get "undifind".

i read some post but didn't get answer : method find javascript deep array object RN react native , How to filter array of objects in react native? , Finding an object in array and taking values from that to present in a select list , How to find object in array and show it in React component? how to fix it?? thank for helping.

updtade: this is my reducer:

import {GET_HARDWARE_START,GET_HARDWARE_SUCSSES,GET_HARDWARE_FAILED} from '../types';
let initialState = {
    data:'',
    isLoading:false,
    error:null
}

export default user = (state=initialState,action)=> {
    switch (action.type) {
        case GET_HARDWARE_START:
           return Object.assign({},state,{isLoading:true})  
         case GET_HARDWARE_SUCSSES:
                //console.log("this log from reducer:" + action.payload)
               // return action.payload
               return Object.assign({},state,{data: action.payload ,isLoading:false})   
        case GET_HARDWARE_FAILED:
           return Object.assign({},state,{error:action.payload,isLoading:true})  
        default:
            break;
    }
    return state
}

and this is my conole.log in 'updateSearch' methode : https://i.sstatic.net/iycqW.png

console log updateSearch

UPDATE 2: completed data get from API :

0:
CONTACTPERSONID: 2017011300
ENABLEPOLL: 0
HARDWAREID: 409792319815
LANDMARKID: 0
MESSAGETIME: "1دقيقه پيش"
MOVINGSTATE: "m"
NICKNAME: "ایران 78 - 875 ع 73"
POSDESCRIPTION: "ايران: استان كرمان - محور انار به شهربابك - 72 كيلومتر از  انار"
SENTDATE: "17:39"
SENTDATE1: "1398-06-23 17:39:16"
SIGNATURE: "1-Normal"
SPEED: 95
TRUCKSTATE: "در حال حرکت"
VEHICLETYPE: 0
XPOINT: 55.026634
YPOINT: 30.249145
8
  • The issue is that, the api calls are asynchronous, so you won't get the data right away. If you console.log(this.props.data) in the updateSearch, what do you see? Commented Sep 11, 2019 at 10:43
  • Can you also post the code of your reducer? Commented Sep 11, 2019 at 10:43
  • yes i get my data in componentDidMount get data correctly @paruchuri-p Commented Sep 11, 2019 at 10:51
  • And you sure that your data satisfies this condition? element.POSDESCRIPTION.i === search;? Commented Sep 11, 2019 at 11:27
  • Can you share the data and search key? Commented Sep 11, 2019 at 11:27

1 Answer 1

0

UPDATE :

i change code and get true :

const data = _.filter(this.props.data,user=>{  
           console.log(user.POSDESCRIPTION)
         if (user.POSDESCRIPTION.includes(search))  {

             return true
         }else {
             return false
         }
        });
        console.log("data serach is : " +JSON.stringify(data) )
       // this.setState({activeSections:data})
        this._updateSections(data)
        this.setState({search:search})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.