javascript - 为什么 find 函数在比较 Vue 中的两个数据属性时不起作用?
问题描述
当Id
ofstateGDP
和rank
of的值statesJson
相等时,它们应该显示为输出。但是有一些我没有得到的错误。
var app = new Vue({
el: "#app",
data(){
return {
statesJson:{
"type": "FeatureCollection",
"features": [{ "rank":"1", "NAME_1": "Andaman and Nicobar"},
{ "rank":"2", "NAME_1": "Rajasthan"},
{ "rank":"3", "NAME_1": "Orissa"}]
},
stateGDP:[{ "Id":"1", "NAME_1": "Andaman and Nicobar","2000":"48"},
{ "Id":"2", "NAME_1": "Rajasthan","2000":"87"},
{ "Id":"3", "NAME_1": "Orissa","2000":"25"}]
}
},
computed:{
check(){
let x = this.statesJson.features.map(feature =>{ return feature.rank})
let stateData = this.stateGDP.find(state => state.Id === x)
return x
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div v-for="state in check">
{{state}}
</div>
</div>
解决方案
这是因为 map 返回一个数组。要修复您的代码,您所要做的就是检查是否x
包含您的 id。
var app = new Vue({
el: "#app",
data() {
return {
statesJson: {
"type": "FeatureCollection",
"features": [
{ "rank":"1", "NAME_1": "Andaman and Nicobar"},
{ "rank":"2", "NAME_1": "Rajasthan"},
{ "rank":"3", "NAME_1": "Orissa"},
]
},
stateGDP: [
{ "Id":"1", "NAME_1": "Andaman and Nicobar","2000":"48"},
{ "Id":"2", "NAME_1": "Rajasthan","2000":"87"},
{ "Id":"3", "NAME_1": "Orissa","2000":"25"},
],
}
},
computed:{
check(){
let state = this.statesJson.features.map(feature => {
return this.stateGDP.find(state => state.Id === feature.rank);
});
return state;
}
},
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<strong>Total object</strong><hr/>
<div v-for="state in check">
{{ state }}
</div>
<br/>
<strong>Seperate variables</strong><hr/>
<div v-for="state in check">
Id = {{ state["Id"] }}<br/>
Name_1 = {{ state["NAME_1"] }}<br/>
2000 = {{ state["2000"] }}
</div>
</div>
推荐阅读
- spring-cloud-config - Spring Cloud 配置服务器中的配置导入
- python - soup.find_all 找不到正则表达式 - regex101 确实
- jquery - 使用 Cheeriogs 从两个元素之间的行中提取文本
- java - 无法使后台服务“无法杀死”
- excel - 如何保持 vba 列表框中的文本值不转换为指数
- r - 在 R 中进行表操作时,求值嵌套太深错误
- reactjs - AG-GRID 不是 gridApi 上的 applyTransaction 方法
- java - 对使用 grails 分页感到困惑
- spatial - 多年数据的空间自相关
- android-studio - 如何解决 RangeError(index): Invalid value: Valid value range is empty: 1 in Flutter/Dart