0

I was wondering if there is a way to order data coming from an api? I'm getting a list of items from an api and I want to push the items that contains a specific key in its object at the end of the list, and just leave the items that doesn't contain that key as they are, is there a way that I can do that from the client side?

{this.state.items.map(i => <Item key={i.id}  button={this.state.button} id={i.id} name={i.name} price={i.price} quantity={i.quantity} />)}
develop05
  • 487
  • 1
  • 9
  • 23
  • Please share your code you currently have and please be more specific how your end result should look like – Simon Franzen Oct 25 '18 at 16:38
  • But yes, you could retrieve a list of items, save some of the items keys in another list and send this to where ever you want. – Simon Franzen Oct 25 '18 at 16:39
  • Thank you, but what I'm trying to do is that for example if I'm getting these two item objects {name: example, img: "", id: "", someKey: ""}, {name: example, img: "", id: ""}. by default the items will show in the same order as they are. but what I want to do is that I want the object that contains "someKey", to show up after the item that doesn't contain that key. – develop05 Oct 25 '18 at 16:47
  • 1
    I think you are looking for the `sort` method on arrays. – Evert Oct 25 '18 at 16:55
  • 1
    You could sort the array after that attribute. Have you read this https://stackoverflow.com/a/1129270/1625253 – Simon Franzen Oct 25 '18 at 16:56

0 Answers0