4

I have array of cities with objects like this:

[{id: 1, name: 'New York'}, {id: 2, name: 'London'} ...]

and I have a value of id.

I put elements (names) from array to select list. But I need to add first option with value from array (name) which have the corresponding id which I can't make it work.

My component:

const propTypes = {  
  cities: PropTypes.array,  
  enteredEvent: PropTypes.array  
};

class newEvent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      showInOption: ''
    };
    this.handleDictionary = this.handleDictionary.bind(this);
    this.searchTypeEvent = this.searchTypeEvent.bind(this);
  }

  componentDidMount() {
    this.searchTypeEvent();
  }

  handleDictionary(e) {
    ...
  }

  searchTypeEvent() {
    if (this.props.enteredEvent.cityid !== null && this.props.enteredEvent.cityid !== undefined) { // if I have id value
      const type = this.props.cities.find(el => el.id === this.props.enteredEvent.cityid); // find an object with id
      this.setState({ showInOption: type.name });
    }
    this.setState({ showInOption: 'Select something' });
  }

  render() {
    return (
      <div>       
        <select onChange={this.handleDictionary}>
          <option>{this.state.showInOption}</option> // here I need to add a value
          {this.props.cities.map(item => {   // here I call other options
            return (<InputSelect key={item.id} directory={item}/>);
          })}
        </select>        
      </div>
    );
  }
}

I have an error:

Uncaught TypeError: Cannot read property 'name' of undefined

How can I fix it?

10
  • You try to find an item by a search term by looking at the ids, is that intended? el.id === this.props.enteredEvent.cityname. You must also make sure that you actually find a type before trying to access name on it. Commented Nov 13, 2018 at 14:38
  • It's unknown what cityname. If it derives from an input, it's unlikely that it is a number that can pass === check. Commented Nov 13, 2018 at 14:39
  • @Tholle this.props.enteredEvent.cityid is correct. My mistake in post. I get object which I need (after find), but I can't get name value Commented Nov 13, 2018 at 14:51
  • @estus this.props.enteredEvent.cityid is correct. My mistake in post Commented Nov 13, 2018 at 14:51
  • Write console.log(type); right after the find and you will get undefined as the error says. I think it's a good idea to guard against the case when the cityid doesn't match a cities object id. Commented Nov 13, 2018 at 14:55

2 Answers 2

6

It seems you need filter.It will return an array of object

let a = [{
  id: 1,
  name: 'New York'
}, {
  id: 2,
  name: 'London'
}]

function b(idToSearch) {
  return a.filter(item => {
    return item.id === idToSearch
  })
};

console.log(b(1))

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

1 Comment

I need to print name value. How can I do it?
4

Try this

  const type = Array.isArray(this.props.cities) && this.props.cities.find(el => el.id ==this.props.enteredEvent.cityid);
  this.setState({ showInOption: type ? type.name : null });

Make sure the name you enter matches with any one of the object in array

3 Comments

When I call this function from option and in function I return value, this function called 6 times, and in last call I get the object value (I catch it, using console.log). Maybe React doesn't have time to perform this function?
no, it isn't help for my. I think there is asynchronous React problem
Worked perfect for me ! thanks very much! Exactly what I wanted !! :)

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.