简体   繁体   English

Vue.js - 通过计算属性从数组中获取唯一值

[英]Vue.js - Unique values from an array through a computed property

I am using a computed property to filter through an array of values and pull out only a set of unique items from within the array.我正在使用计算属性来过滤一组值并仅从该数组中提取一组唯一项。 Originally I only needed it for one variable but I need to use it to pull two different values now and can't get it to work independently of each other when making two computed properties.最初我只需要一个变量,但我现在需要使用它来提取两个不同的值,并且在创建两个计算属性时无法使其彼此独立工作。

Here's my Vue HTML code:这是我的 Vue HTML 代码:

<div v-for="item in getUniques">
<input :value="item.car" v-model="selectedCars" type="checkbox">
<label>&nbsp;{{item.car}}</label>
</div>

Here's the function:这是函数:

 data(){
      return {

selectedCars: [],
prefetch: [
           {car: "XC90", brand: "Volvo"},
           {car: "XC60", brand: "Volvo"},
           {car: "XC90", brand: "Volvo"},
           {car: "X-Type", brand: "Jaguar"}   
          ]
 }
},


computed: {

getUniques(){

                return this.prefetch.reduce((seed, current) => {

                    return Object.assign(seed, {
                        [current.car]: current
                    });

                }, {});

            },

}


// This works perfectly for pulling out the unique values of cars and returns... [XC90,XC60, X-Type] 

But when I try to create another computed property to do the same, but only work with brands , everything breaks and I get undefined.但是当我尝试创建另一个计算属性来做同样的事情,但只与品牌一起工作时,一切都会中断,我变得不确定。 How can I tweak this computed property so I can filter out all the other unique values in this array?我如何调整这个计算属性,以便我可以过滤掉这个数组中的所有其他唯一值?

I've tried everything and can't work it out.我已经尝试了一切,但无法解决。 Thanks in advance for any help!!在此先感谢您的帮助!!

For the basic check for uniques对于唯一性的基本检查

For an array like:对于像这样的数组:

let prefetch = [
  {car: "XC90", brand: "Volvo"},
  {car: "XC60", brand: "Volvo"},
  {car: "XC90", brand: "Volvo"},
  {car: "X-Type", brand: "Jaguar"}   
 ]

You could use 2 the filter function as stated here: Get all unique values in a JavaScript array (remove duplicates)您可以使用 2 此处所述的过滤器函数: Get all unique values in a JavaScript array (remove duplicates)

 let prefetch = [ {car: "XC90", brand: "Volvo"}, {car: "XC60", brand: "Volvo"}, {car: "XC90", brand: "Volvo"}, {car: "X-Type", brand: "Jaguar"} ] function getUniqueCars() { return prefetch.map(x => x.car).filter((v,i,s) => s.indexOf(v) === i) } function getUniqueBrands() { return prefetch.map(x => x.brand).filter((v,i,s) => s.indexOf(v) === i) } console.log(getUniqueCars()) console.log(getUniqueBrands())

This also works as a computed function within vue...这也可以作为 vue 中的计算函数...

A solution for vue using a temporary Array vue 使用临时数组的解决方案

Another solution could be the creation of a temporary Array to calculcate the unique car/brand pairs you want to use...另一种解决方案可能是创建一个临时数组来计算您要使用的独特汽车/品牌对...

 let vm = new Vue({ el: '#app', data() { return { selectedCars: [], prefetch: [{ car: "XC90", brand: "Volvo" }, { car: "XC60", brand: "Volvo" }, { car: "XC60", brand: "Volvo" }, { car: "XC90", brand: "Volvo" }, { car: "X-Type", brand: "Jaguar" } ] } }, computed: { getUniques() { let tempArray = []; for (let item of this.prefetch) { (tempItem => { if (!(tempItem.length > 0 && tempItem.find(x => x.brand === item.brand))) tempArray.push(item); })(tempArray.filter(x => x.car === item.car)) } return tempArray; } } })
 <div id="app"> <div v-for="item in getUniques"> <input :value="item.car" v-model="selectedCars" type="checkbox"> <label>&nbsp;{{item.brand}} {{item.car}}</label> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

you can use lodash's uniq你可以使用 lodash 的 uniq

and do并做

return _.uniqBy(this.prefetch, 'car');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM