首页 > 解决方案 > 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]或基于这个我想检索给定文本的值。

标签: javascriptvue.js

解决方案


创建一个方法并使用此代码找出匹配的方法。

function setSelectedValue() {
    let matchingDatSet = this.datasets.find(ele => ele.text == this.selectedText);
    if(matchingDataSet !== undefined) {
        this.selectedValue = matchingDataSet.value;
    }
}

推荐阅读