如何检查数组是否包含来自vue.js模板中另一个数组的项目

时间:2019-06-23 00:32:04

标签: javascript vue.js

我正在尝试弄清楚如果job中的job.name也位于anotherArray中,那么如何显示job.name。

    <li v-for='job in jobs'>
    <template v-if="job.name in anotherArray"
      {{ job.name }}
    </template>
    </li>

请注意anotherArray的结构与Jobs数组相同。所以我想检查另一个数组中的element.name

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以使用计算得出a = list of dictionaries if arg1 is None and arg2 is None: result = a elif arg1 is not None and arg2 is not None: result = [d for d in a if d['key1'] == arg1 and d['key2'] == arg2] elif arg1 is not None and arg2 is None: result = [d for d in a if d['key1'] == arg1] elif arg1 is None and arg2 is not None: result = [d for d in a if d['key2'] == arg2] 的列表,然后创建一种使用includes进行检查的方法

anotherArrayName

和在模板中

computed: {
    anotherArrayName() {
        return this.anotherArray.map(item => item.name)
    }
},
methods: {
    isInclude(name) {
        return this.anotherArrayName.includes(name)
    }
}