I'm embarrassed to post this but I could really use a hand here. This code just looks nasty. I have a feeling that I can write a cleaner approach with filter
or reduce
but can't seem to stab it. Any thoughts, community?
const vin = detections.map(detection => {
return detection.textAnnotations.map(v => {
let n = v.description.replace(/\s+/g, '');
if (n.length === 17) {
return n;
}
});
})[0][0];
Thanks!
Just an attempt to refactor your code:
const getMatchedTextAnnotation = (textAnnotation) => {
const n = textAnnotation.description.replace(/\s+/g, '');
return n.length === 17 ? n : null;
}
const extractAnnotationsFromDetection = (detection) => {
return detection.textAnnotations.reduce((arr, textAnnotation) => {
const n = getMatchedTextAnnotation(textAnnotation);
return !!n ? [ ...arr, n] : arr;
}, [])
}
const vinArr = detections.reduce((arr, detection) => {
const subArr = extractAnnotationsFromDetection(detection);
return !!subArr ? [ ...arr, ...subArr ] : arr;
}, [])
const vin = !!vinArr ? vinArr[0] : null;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.