Using JS in this JSON api

so the correct answer I need here is “response[6]” but I want to make it a JS function that can find it by the “id” and then return the “price_usd”

because this is the “stratis” (object?) id one, and I want to be able to always find the price for it, but sometimes its rank changes so it will be response[6], sometimes its response[5], etc depending on its performance

so how can I make a JS function that always finds the id: “stratis” one (instead of using response[6]) and return “price_usd” ? thanks!

You can use the filter() array method to return only the matching object.

.filter() would need to loop over the entire array every time. You’d be better off using a loop you could exit early on.

const getItemById = (arr, targetId) => {
  for(const i = 0; i < arr.length; i++) {
    if(arr[i].id === targetId) {
      return arr[i]

  return undefined

I did consider that, but depending on what the OP is building that could just end up being premature optimization (at the expense of readability).

1 Like

Yeah, either way is fine if the list is small enough. But I think it’s probably just as much effort.


const result = array.find(obj => === searchValue)



This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.