javascript - 迭代数据脚本中的键
问题描述
我试图弄清楚如何将下拉菜单中的用户输入值与 ID 对象与 Vue 中与下拉菜单选项匹配的键进行比较。
例子:
<template>
<select v-model="selectMenu">
<option v-for"select in selections">{{ selection }}</option>
</select>
</template>
<script>
export default {
data() {
return {
selectMenu: '',
selections: [ 'one', 'two', 'three' ],
ids: {
one: 'dfs745jfdb',
two: 'adfjdsh3gf5',
three: 'ag23dsgnsj'
}
}
}
}
</script>
我想出了一个更简单的方法。我对 vue 和编码很陌生。我所做的是将选择和 id 组合成一个数组,如下所示 :
<template>
<select v-model="selectMenu">
<option v-for"selectId in selectIds" v-bing:value="selectId.id">
{{ selectId.text }}
</option>
</select>
</template>
<script>
export default {
data() {
return {
selectMenu: '',
selectIds: [
{ text: 'one', id: 'dfs745jfdb' },
{ text: 'two' id: 'adfjdsh3gf5' },
{ text" 'three' id: 'ag23dsgnsj' }
]
}
}
}
</script>
解决方案
this.ids[this.selectMenu]
应该给你 ids 对象中的对象。
推荐阅读
- javascript - 有没有办法将长度大于 15 的字符串转换为数字,而不进行四舍五入?
- c - 在数组中查找最小偶数和最大偶数
- java - 如何故意让 kubernetes 的工作失败?
- grouping - 如何在列表和标签中为多个级别的组创建小计
- javascript - Modernizr 的问题 -> ReferenceError:Modernizr 未定义(Firefox 控制台)
- javascript - Angular 7 js2xmlparser TypeError:处理程序不是函数
- python - 如何在小于N的数组中找到最大和
- java - 获取无法编译内联过滤器:
- javascript - 如何使用按钮单击列出存储在 JS 数组中的所有值?
- javascript - 编剧错误:UnhandledPromiseRejectionWarning:TimeoutError:等待firstPage代理失败:超时30000ms