Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the index of selected element in react native array

I want an index of the selected element in the react native array. I tried with indexOf() but it always returns -1. School is my array & email contains the value of the element whose index to find.

deleteDetails = (email) => {
  var index = School.indexOf(email);
}
like image 819
Fahad Avatar asked Oct 04 '18 07:10

Fahad


People also ask

How do you find the index of an element in array React native?

You can use debugger or just simply add console. log before calling indexOf. If your email element is string "[email protected]", your array should contain string elements. Edit: Considering comments below, your callback method should return the index of the element.

How do you find the index of an object in an array React?

To find the index of an object in an array, by a specific property: Use the map() method to iterate over the array, returning only the value of the relevant property. Call the indexOf() method on the returned from map array. The indexOf method returns the index of the first occurrence of a value in an array.

How do I get the index of a clicked element in React?

To get the key index of an element on click in React:Add an onClick event listener to each element. Every time an element is clicked, call the handler function passing it the event and the key index.

How do you find the index of an array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.


3 Answers

As per your comment, School is an array of object, hence you need to use findIndex instead of indexOf

var arr = [{
  email: "[email protected]",
  remarks: "tt",
  slno: 1
}, {
  email: "[email protected]",
  remarks: "ttt",
  slno: 2
}, {
  email: "[email protected]",
  remarks: "tttt",
  slno: 3
}, {
  email: "[email protected]",
  remarks: "4",
  slno: 4
}];

function getIndex(email) {
  return arr.findIndex(obj => obj.email === email);
}

console.log(getIndex("[email protected]"));

Hope this will help!

like image 186
Prasun Avatar answered Oct 10 '22 22:10

Prasun


https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

According to documentation, indexOf returns -1 only when value is not found in array. Check your School array and email value before calling indexOf. You can use debugger or just simply add console.log before calling indexOf.

deleteDetails = (email) => {
  console.log(email);
  console.log(School);
  var index = School.indexOf(email);
}

If your email element is string "[email protected]", your array should contain string elements.

var email = "[email protected]";
var emails = ["[email protected]", "[email protected]"];
console.log(emails.indexOf(email));
// expected output: 0

Edit: Considering comments below, your callback method should return the index of the element. So deleteDetails function should be like this.

deleteDetails(email, index) {
      // code
    }

Let's consider Flatlist for listing.

<FlatList
        data={this.props.data}
        extraData={this.state}
        keyExtractor={this._keyExtractor}
        renderItem={this._renderItem}
      />

_renderItem = ({item, index}) => (
<MyListItem
  id={item.id}
  onPressItem={this._onPressItem}
  selected={!!this.state.selected.get(item.id)}
  title={item.title}
  onDeleteButtonPressed={(item, index) => this.deleteDetails(item,index)}
/>
);

And you must create an onDeleteButtonPressed prop and call it when button pressed in component to trigger deleteDetails function.

like image 30
Sedat Öztürk Avatar answered Oct 10 '22 22:10

Sedat Öztürk


If You are using Flatlist then;

renderDataItem = ({item, index}) => {
return (
  <TouchableWithoutFeedback
    onPress = {() => Actions.testing2({ sayi : index })}>
    <View style = {styles.allCointer}>
      <View style = {styles.textContainer}>
        <Text style={styles.textContainerTitle}> {item.Title} </Text>
        <Text style={styles.textContainerText}> {item.Description} </Text>
      </View>
      <Image
        style = {styles.image}
        source = {{uri : item.Files[0].FileUrl}}
      />
    </View>
  </TouchableWithoutFeedback>
)}

index parameter in that line,

renderDataItem = ({item, index}) => {

is your current index value.

like image 26
Muhammet ALAPAN Avatar answered Oct 10 '22 21:10

Muhammet ALAPAN