【问题标题】:Filter the array based on another array Vuejs根据另一个数组Vuejs过滤数组
【发布时间】:2020-04-30 01:49:37
【问题描述】:

我有两个数组

 {
"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时,我想将products中的frequency中的数字与haufigkeit中的id进行比较,当它们匹配时,然后显示@987654330中的name @

这是我正在尝试的

computed:{
selectFrequency:function(){
    let results= this.haufigkeit.filter(array=>array.every(item => this.products.filter(group=>group.frequency.includes(item))));
}
}

我已经尝试了两天,它给了我一个错误cannot read property 'every' of undefined。谁能建议我在哪里做错了?

【问题讨论】:

  • haufigkeit.filter(array=&gt;array.every(item ... 在 haufigkeit.filter 函数中,array 是数组中的一个单独项,因此它是一个具有键名称、值和 ID 的对象。通过执行array.every,您希望数组是一个数组,但实际上它不是......

标签: javascript arrays vue.js vuejs3


【解决方案1】:

您可以使用Javascript some 函数。在下面的示例中,我返回了 haufigkeit 数组的 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());

【讨论】:

    【解决方案2】:

    编辑:好的,现在我明白了,这样的东西应该适合你:https://jsfiddle.net/q9grc04s/

    如果您选择一个产品,您会看到它显示任何具有包含在所选频率中的 ID 的 aufigkeit。

    <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 fiddle 链接是一个有效的解决方案。

    【讨论】:

    • 嗨,我已经更正了我的帖子。我正在使用this.,但仍然出现同样的错误。
    • 这是一个很好的工作解决方案。非常感谢...我需要在您的解决方案中进行一个小更正。我也可以通过像这样将常量结果更改为const results = this.haufigkeit.filter(x =&gt; this.selectedFrequency.frequency.includes(x.id)) 来使用v-bind:value="level1" 而不是v-bind:value="level1.frequency" 来选择属性吗?我想这样做是因为,在某些时候,我还必须基于选定的 ````Jivi``` 来渲染 Hint 属性。
    • 是的!这对我帮助很大。我已经尝试过这种方法,但是我声明了 selectedProduct: [ ] 而不是 null ,这给我带来了错误。非常感谢。
    • 乐于助人 :-)
    猜你喜欢
    • 1970-01-01
    • 2014-05-03
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多