javascript,我现在有个Map结构,存储的key对应的value是一个mesh数组,我现在拿到了一个mesh,我想通过uuid去寻找,他在那个key对应的value数组中,如果找到了,返回对应key值
function findKeyByMeshUuid(map, uuid) {
for (let [key, meshes] of map.entries()) {
if (meshes.some(mesh => mesh.uuid === uuid)) {
return key;
}
}
return null; // 如果没有找到,返回 null
}
// 示例用法:
// 假设有一个 Map 结构,其中的 value 是包含 mesh 对象的数组
const myMap = new Map();
// 填充一些模拟数据
myMap.set('key1', [{ uuid: '123' }, { uuid: '456' }]);
myMap.set('key2', [{ uuid: '789' }, { uuid: '101112' }]);
// 某个待查询的 mesh 的 uuid
const searchUuid = '789';
// 执行查找
const foundKey = findKeyByMeshUuid(myMap, searchUuid);
// 输出查找结果
if (foundKey) {
console.log(`Found in key: ${foundKey}`);
} else {
console.log('Mesh not found');
}
随便记录一下用法吧,
function findKeyByMeshUuid(map, uuid) {
for (let [key, meshes] of map.entries()) {
//map.entries 返回了键值对可迭代对象,使用for of..
if (meshes.some(mesh => mesh.uuid === uuid)) {
//Array.some()用于过滤·
return key;
}
}
return null; // 如果没有找到,返回 null
}
Array.some()
some() 方法检查某些数组值是否通过了测试。
这个例子检查某些数组值是否大于 18:
实例
var numbers = [45, 4, 9, 16, 25];
var someOver18 = numbers.some(myFunction);
function myFunction(value, index, array) {
return value > 18;
}