javascript - Match with multiple arrays
问题描述
I start with multiple arrays. Then based on the checkbox selection I would like to crosscheck the value with the item
array and get the relevant values from the price
and exp
arrays.
<input type="checkbox" name="grocery" value="peach">
<input type="checkbox" name="grocery" value="apple">
<input type="checkbox" name="grocery" value="orange">
example if the 2nd check box is checked (value="apple") then I need to be able to get its values separately.
This is what I have so far. How do I match the selected checkbox values to the item
array to be able to gather the values from the price
, exp
and lot
arrays? I am looking for a way to do it in vanilla javascript.
let item = ["peach", "apple", "orange"];
let price = [21, 32, 18];
let exp = [21, 18, 25];
let lot = [6, 8, 4];
let crossCheck = [];
let items = document.getElementsByName('grocery');
for(var i = 0; i < items.length; i++) {
if(items[i].type == 'checkbox' && items[i].checked) {
crossCheck.push(items[i].value);
console.log(s)
}
}
解决方案
The best way I think you using the array of object.
let array = [
{ "item": "peach", "price": 21, "exp": 21, "lot": 6 },
{ "item": "apple", "price": 32, "exp": 18, "lot": 8 },
{ "item": "orange", "price": 18, "exp": 25, "lot": 4 }
]
after define you can add to crossCheck
crossCheck.push(array[i])
Hope this solution can help you. Thanks for reading
推荐阅读
- git - Git将分支合并到父亲并恢复提交
- discord.js - Discord.js V13 使用斜线命令时添加角色
- github - 有哪些方法可以在不下载到本地机器的情况下将数据从 gdrive 导入 colab 并使 notebook 可共享?
- python - 使用python根据某些值过滤嵌套的json
- python - 从字符串中提取数据
- python - numpy 可以对字典对象列表进行排序,还是 pandas 数据框操作是我唯一的选择?
- python - 遍历字符串列表:错误消息
- python - 打开电子邮件时触发Python Outlook win32事件
- python - 如何使用 ruamel.yaml 读取多个 yaml 定义
- pandas - 从包含所有行的数据框中选择两组列