I am trying to return a single value from a filter that returns a large object.
return data.filter(subject => subject.id === 1)
.map((subject) => {
return subject.total.toString();
});
I have tried, toString, JSON.parse and a few more but always get it either as a single array value.
[112]
or a string inside the array
["112"]
but not a single returned value
112
Is map the wrong method? How do I return a pure integer or string would do?
Instead of filter
which returns an array with filtered values, use find
:
const subject = data.find(subject => subject.id === 1);
return subject.total.toString();
or shorter:
return data.find(subject => subject.id === 1).total.toString();
You just need to pick the first element. This should suffice..
return data.filter(subject => subject.id === 1)[0].total+""
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With