Reactjs: Sort Option Choices From Api June 22, 2024 Post a Comment I have a select and it gets the options from the api, and it looks like this: I want to sort them in ascending form but I don't know how to do that, here is my code Solution 1: You could write a function that sorts your data which you can then render. To optimise it further you can memoize this function sortData = (array) => { return array.sort(function(a, b) { a.match(/(\d+) months/i)[1] - b.match(/(\d+) months/i)[1] }) } render() { const data = this.sortData(this.state.variantsData.subscription_plan); return ( <select className="form-control form-control-sm" name="subscriptions" onChange={this.changeValues}> {this.state.variantsData.subscription_plans.map((item, i) => ( <option key={i} value={item.uuid}>{item.name}</option> )) } </select> ) } Copy Share Post a Comment for "Reactjs: Sort Option Choices From Api"
Post a Comment for "Reactjs: Sort Option Choices From Api"