Javascript VueJs中数据对象的匹配值
如何从Vue中的数据对象检索数据 我有以下格式的数据:Javascript VueJs中数据对象的匹配值,javascript,vue.js,Javascript,Vue.js,如何从Vue中的数据对象检索数据 我有以下格式的数据: datasets: [{ text:"Cars", value: "[1,2,3]" }, { text:"Trains", value: "[1,4,10] } ] 现在,我从路线道具获得以下信息: this.selectedText= this.$route.name; 其中,这个.route.name是例如“Cars”。 现在我想使用this.selectedValue从这个数组中获取相应的值: 因此,如果this.se
datasets: [{
text:"Cars",
value: "[1,2,3]"
},
{
text:"Trains",
value: "[1,4,10]
}
]
现在,我从路线道具获得以下信息:
this.selectedText= this.$route.name;
其中,这个.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;
}
}