I'm using a nodejs speech recognition API that returns a string with multiple texts inside a JSON like structure which is invalid and unusable
{
"text": "Play"
}
{
"text": "Play astronaut"
}
{
"text": "Play astronaut in the"
}
{
"text": "Play astronaut in the ocean"
}
{
"entities": {},
"intents": [],
"text": "Play astronaut in the ocean.",
"traits": {}
}
and I only want last element which is
{
"entities": {},
"intents": [],
"text": "Play astronaut in the ocean.",
"traits": {}
}
by filtering it out or something to extract the text "Play astronaut in the ocean."
The API always outputs multiple text elements in different quantities each time, but I want the last element is that possible? or should I find a different API that's usable?
Assuming the response is returned as an array, ie [{ "text": "Play" }, { next one.. }] etc
Then you can get the last element in the array by variablising the array, get its length, and find the last object.
e.g.
const x = [{ text.. }, { text.. }]
const lastItem = x[x.length - 1]
Then if you also want to find the last item within that object, you could also do that with Object.values
const lastItemObject = Object.values(lastItem)
const lastItemInTheObject = lastItemObject[lastItemObject.length - 1]