javascript - 如何将数字与香草JavaScript中的数组中的数字匹配?
问题描述
我正在 JS 5 中制作一个过滤器。
简而言之:Bubble 有 ids、attributes。我获取气泡 ID,并将其与对象数组匹配。如果气泡 id 与对象数组中的一个 id 匹配,我想显示对象信息/id。
用户单击气泡,我得到气泡 ID 并将其存储。
然后需要在对象/JSON 中比较气泡 ID,如果气泡 ID 与对象中的项目之一相同,则应显示对象 ID 及其信息。
到目前为止,我设法获得了 obj ID,以及我认为的数组中的数字,但这似乎不太正确。
我该如何解决这个问题,所以当我单击数字时,它与对象数组中的数字匹配,并采用适当的数字并显示该对象信息?
代码笔: https ://codepen.io/Aurelian/pen/xyVmgw ?editors=0010
JS代码:
var dataEquipmentList = [
{
"equipmentId": 16,
"equipmentTitle": "Horizontal Lifelines & Anchors",
"equipmentMainBgImg": "https://www.yorkshirewildlifepark.com/wp-content/uploads/2016/06/lion-country-banner-1.jpg",
"productList": [
{
"rowId": 1,
"toolList": [
{
"toolPicture": "https://youtube.com/",
"toolName": "Tracoda",
"toolModelNumber": "ET 10",
"toolDescription": "Chest belt fitted",
"toolUrl": "https://google.com/"
},
{
"toolPicture": "https://youtube.com/",
"toolName": "Tracoda",
"toolModelNumber": "ET 10",
"toolDescription": "Chest belt fitted",
"toolUrl": "https://google.com/"
}
]
},
{
"rowId": 2,
"toolList": [
{
"toolPicture": "https://youtube.com/",
"toolName": "Tracoda",
"toolModelNumber": "ET 10",
"toolDescription": "Chest belt fitted",
"toolUrl": "https://google.com/"
}
]
}
]
},
{
"equipmentId": 17,
"equipmentTitle": "DDDDD Lifelines & Anchors",
"equipmentMainBgImg": "http://www.waterandnature.org/sites/default/files/styles/full/public/general/shutterstock_creative_travel_projects.jpg?itok=MsZg16JU",
"productList": [
{
"rowId": 1,
"toolList": [
{
"toolPicture": "https://youtube.com/",
"toolName": "Tracoda",
"toolModelNumber": "ET 10",
"toolDescription": "Chest belt fitted",
"toolUrl": "https://google.com/"
},
{
"toolPicture": "https://youtube.com/",
"toolName": "Tracoda",
"toolModelNumber": "ET 10",
"toolDescription": "Chest belt fitted",
"toolUrl": "https://google.com/"
}
]
},
{
"rowId": 2,
"toolList": [
{
"toolPicture": "https://youtube.com/",
"toolName": "Tracoda",
"toolModelNumber": "ET 10",
"toolDescription": "Chest belt fitted",
"toolUrl": "https://google.com/"
}
]
}
]
}
]
function getEquipmentIds() {
for(var keys in dataEquipmentList) {
var key = dataEquipmentList[keys];
}
}
getEquipmentIds();
//////////
// Mindmap Code
//////////
function filterBubbles() {
var equipmentList = document.querySelectorAll('.pt-bubble');
equipmentList.forEach(function(item) {
var equipmentId = item.getAttribute('data-equipment-id');
item.addEventListener('click', function(){
//console.log(equipmentId);
for(var keys in dataEquipmentList) {
var keyArray = [];
var key = dataEquipmentList[keys].equipmentId;
keyArray.push(keys);
console.log(keyArray);
for (var i=0; i < keyArray.length; i++) {
if (equipmentId === keyArray[i]) {
console.log(equipmentId + " It's a match with " + keyArray[0]);
}
}
// if(equipmentId === keyArray) {
// console.log(equipmentId + "It's a match with " + key);
// } else {
// console.log("else" + key);
// }
}
})
})
}
filterBubbles();
解决方案
function getMatch(data, id) {
// Returns the object that match with the ID
return data[data.map(i=>i.equipmentId).indexOf(id)];
}
console.log(dataEquipmentList, 16);
推荐阅读
- ios - 关闭弹出框后如何调用函数?
- r - Should I put outcome variable in Matchit::matchit ()
- php - Unity WebGL PHP 数据库操作
- python - 将 MongoDB 与 Django 集成
- flutter - 在具有可变高度的小部件树中使用 DefaultTabController
- typescript - 如何返回链式异步函数的结果?
- python - Tensorflow v2 生成器 ValueError:未能找到可以处理输入的数据适配器:
, - java - 无法使用 java 在 Azure 存储 Blob 中检索 Blob 的元数据
- python - Telethon 异步功能到 tkinter 按钮
- python - 如何在python中自动查找列表中元素的开始和结束索引