用js写一个函数,[{"bounds":{"bottom":272,"left":82,"right":160,"top":228},"confidence":0.9984832,"inferenceTime":82.0,"preprocessTime":11.0,"text":"天气","words":"天气"},{"bounds":{"bottom":277,"left":336,"right":417,"top":225},"confidence":0.8993952,"inferenceTime":82.0,"preprocessTime":11.0,"text":"相册","words":"相册"}] 例如传入text的值,返回对应的bounds里的值
时间: 2023-05-21 07:05:00 浏览: 79
svg-path-bounds:获取svg路径数据的边界框
可以使用以下函数来实现:
```javascript
function findBounds(text, data) {
const result = data.find(obj => obj.text === text);
return result ? result.bounds : null;
}
```
该函数接受两个参数:text 和 data。text 是要查找的文本,data 是一个包含对象的数组,每个对象都有一个 bounds 属性和一个 text 属性。函数将搜索 data 数组,找到第一个文本值等于 text 的对象,并返回该对象的 bounds。如果找不到匹配的对象,则返回 null。
例如,传入下面的参数:
```javascript
const data = [{"bounds":{"bottom":272,"left":82,"right":160,"top":228},"confidence":0.9984832,"inferenceTime":82.0,"preprocessTime":11.0,"text":"天气","words":"天气"},{"bounds":{"bottom":277,"left":336,"right":417,"top":225},"confidence":0.8993952,"inferenceTime":82.0,"preprocessTime":11.0,"text":"相册","words":"相册"}];
const text = "天气";
const result = findBounds(text, data);
console.log(result); // 输出: {"bottom":272,"left":82,"right":160,"top":228}
```
该函数将返回一个对象,该对象包含 bounds 的值。
注意:这里的代码仅供参考,实际开发中可能需要添加错误处理等功能。
阅读全文