javascript - VueJs中数据对象的匹配值
问题描述
如何从 Vue 中的数据对象中检索数据?
我有这种格式的数据:
datasets: [{
text:"Cars",
value: "[1,2,3]"
},
{
text:"Trains",
value: "[1,4,10]
}
]
现在我从路线道具中得到以下信息:
this.selectedText= this.$route.name;
例如this.$route.name
在哪里。"Cars"
现在我想用这个this.selectedValue
来从这个数组中获取相应的值:所以如果this.selectedText="Cars"
那时this.selectedValue=[1,2,3]
或基于这个我想检索给定文本的值。
解决方案
创建一个方法并使用此代码找出匹配的方法。
function setSelectedValue() {
let matchingDatSet = this.datasets.find(ele => ele.text == this.selectedText);
if(matchingDataSet !== undefined) {
this.selectedValue = matchingDataSet.value;
}
}
推荐阅读
- html - 如何仅使用 html 或 css 为音频制作切换按钮?
- shopify - 如何删除单词之间的空格 Liquid-C (Shopify)
- javascript - 为什么单击锚链接后我的 jscript 开始出错
- python - TabularDataBunch.from_df 给出没有属性“代码”的错误
- r - 如果我们同时将“字符”和“数字”类型的结果的 ifelse 放在一起,为什么 mutate 不起作用?
- php - 如何在 kubernetes 中拥有可自动扩展的 Laravel API
- mongodb - MongoDB:处理对于 Int64 来说太大的整数
- openlayers - OpenLayers 3 - 仅为特定 bbox 提供 OSM 底图图块
- sql - 在包含多个相关项的 sql 查询中返回 json
- c++ - 不能在带有 vs2017 的 linux 子系统上包含标头