我有两个数组
{
"products": [
{
"name": "Jivi",
"Hint": "45-60 IE/kg alle 5 Tage\n60 IE 1x/Woche\n30-40 IE 2 x/Woche",
"frequency": ["1", "2", "8"]
},
{
"name": "Adynovi",
"Hint": "40-50 IE/kg 2x/Woche im Abstand von 3-4 Tagen",
"frequency": ["2", "6", "7"]
},
{
"name": "Esperoct",
"Hint": "\"50 IE/kg \nalle 4 Tage\"\n",
"frequency": ["7"]
}
],
"haufigkeit" : [
{
"name": "1x / Woche",
"id": 1,
"value": 52.1428571429
},
{
"name": "2x / Woche",
"value": 104.2857142857143,
"id": 2
}
]
}
我有一个使用Vuejs的选择下拉列表,其中products.name
正在呈现。
<select v-model="selectFrequency">
<option v-for="(level1,index) in dataJson.products"
v-bind:value="level1.frequency">{{level1.name}}</option>
</select>
例如,当我选择Jivi
时,我想将frequency
的{{1}}中的数字与products
中的id
中的数字进行比较, ,然后显示haufigkeit
中的name
这就是我正在尝试的
haufigkeit
我已经尝试了两天,但它给我一个错误computed:{
selectFrequency:function(){
let results= this.haufigkeit.filter(array=>array.every(item => this.products.filter(group=>group.frequency.includes(item))));
}
}
。有人可以建议我在哪里做错了吗?
答案 0 :(得分:2)
编辑:好的,现在我了解了,类似这样的方法应该对您有用:https://jsfiddle.net/q9grc04s/
如果选择一个产品,您将看到该产品显示在所选频率中包含ID的所有haufigkeit。
<template>
<div>
<div>
<select v-model="selectedFrequency">
<option
v-for="(level1, i) in products"
:key="i"
:value="level1.frequency"
>
{{level1.name}}
</option>
</select>
</div>
<div>
<h1>Haufigkeit Matches:</h1>
<ul v-if="haufigkeitMatches">
<li v-for="match in haufigkeitMatches">{{ match.name }}</li>
</ul>
</div>
</div>
</template>
<script>
export default {
data: {
selectedFrequency: [],
products: [
{
name: "Jivi",
Hint: "45-60 IE/kg alle 5 Tage\n60 IE 1x/Woche\n30-40 IE 2 x/Woche",
frequency: [1, 2, 8]
},
{
name: "Adynovi",
Hint: "40-50 IE/kg 2x/Woche im Abstand von 3-4 Tagen",
frequency: [2, 6, 7]
},
{
name: "Esperoct",
Hint: "\"50 IE/kg \nalle 4 Tage\"\n",
frequency: [7]
}
],
haufigkeit : [
{
name: "1x / Woche",
id: 1,
value: 52.1428571429
},
{
name: "2x / Woche",
value: 104.2857142857143,
id: 2
}
]
},
computed: {
haufigkeitMatches(){
return this.haufigkeit.filter(x => this.selectedFrequency.includes(x.id))
}
}
}
</script>
注意:抱歉,所有编辑我都想掌握stackoverflow编辑器,但是JS小提琴链接是一个有效的解决方案。
答案 1 :(得分:1)
您可以使用Javascript some功能。在下面的示例中,我将返回haufigkeit数组的ID,该ID等于产品的频率
var data = {
"products": [
{
"name": "Jivi",
"Hint": "45-60 IE/kg alle 5 Tage\n60 IE 1x/Woche\n30-40 IE 2 x/Woche",
"frequency": ["1", "2", "8"]
},
{
"name": "Adynovi",
"Hint": "40-50 IE/kg 2x/Woche im Abstand von 3-4 Tagen",
"frequency": ["2", "6", "7"]
},
{
"name": "Esperoct",
"Hint": "\"50 IE/kg \nalle 4 Tage\"\n",
"frequency": ["7"]
}
],
"haufigkeit" : [
{
"name": "1x / Woche",
"id": 1,
"value": 52.1428571429
},
{
"name": "2x / Woche",
"value": 104.2857142857143,
"id": 2
}
]
};
var result = [];
function selectFrequency(){
data.products.forEach(elem => {
elem.frequency.forEach(fre =>{
var arr = data.haufigkeit;
if(arr.some(arr => arr.id == fre))
result.push(fre);
})
});
return result;
}
console.log(selectFrequency());