score:0

Accepted answer

in here

result.foreach((el) => {
  if (el === "undefined") {
    return;
  } else {
    console.log(el) // this provides the correct values
    setnearestplace(el); // here is where it wont set my state to the values
    console.log(nearestplace);
  }
  return el;
});

you are calling setnearestplace() in a loop and passing the array element as an argument. as a result, the last value passed, i.e. the last element of the array will be your final state.

i believe you are trying to do something like this instead

setnearestplace(result.filter(ele => boolean(ele))); // boolean(ele) check whether the ele is null or undefined.

Related Query

More Query from same tag