9

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);
}
4
  • Can you please share the content of your School array? Commented Oct 4, 2018 at 8:44
  • @PrasunPal School array contains of email ids Commented Oct 4, 2018 at 9:08
  • Can you please share the output of console.log(School) value, I just wanted to know whether it is string or object. Commented Oct 4, 2018 at 14:20
  • 0: {email: "[email protected]", remarks: "tt", slno: 1} 1: {email: "[email protected]", remarks: "ttt", slno: 2} 2: {email: "[email protected]", remarks: "tttt", slno: 3} 3: {email: "[email protected]", remarks: "4", slno: 4} success: "true" length: 4 proto: Array(0) Commented Oct 5, 2018 at 3:44

3 Answers 3

18

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!

Sign up to request clarification or add additional context in comments.

1 Comment

const array1 = [5, 12, 8, 130, 44]; const isLargeNumber = (element) => element > 13; console.log(array1.findIndex(isLargeNumber)); This link was helpful developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
4

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.

4 Comments

still returns -1, i tried with following var email1 = email; console.log(School); var emails = School; console.log(emails.indexOf(email1)); and console result is: 0: {email: "[email protected]", remarks: "tt", slno: 1} 1: {email: "[email protected]", remarks: "ttt", slno: 2} 2: {email: "[email protected]", remarks: "tttt", slno: 3} 3: {email: "[email protected]", remarks: "4", slno: 4} success: "true" length: 4 proto: Array(0) -1
If i'm reading this right, your school array is containing objects, not just string or numbers. Can you write your console.log(email) output?
actually it is displayed as a list with a remove icon, when i click to remove second email the console.log(email) output is [email protected]
your array contains objects but you are trying to find string inside and object array. You can't find. I'm writing a potential answer above.
1

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.

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.