uniapparrid1name111id2name222怎么根据shopId=1返回对应的name
你可以使用数组的 find 方法来查找符合条件的元素,并返回对应的 name 值。以下是示例代码:
const arr = [{id:1,name:'111'},{id:2,name:'222'}];
const shopId = 1;
const result = arr.find(item => item.id === shopId);
if (result) {
console.log(result.name); // 输出:111
} else {
console.log("未找到对应的数据");
}
在上述代码中,我们使用了 find 方法来查找数组 arr 中 id 等于 shopId 的元素。如果找到了匹配的元素,就输出对应的 name 值。如果没有找到匹配的元素,就输出一个提示信息。
原文地址: https://www.cveoy.top/t/topic/iqAf 著作权归作者所有。请勿转载和采集!