I have the array of objects and i am trying to show the records based upon some filtration,
I am doing in VUE
Here is my code
return this.system.filter(function (item, key) {
if(key.)
});
what i am tryin to achieve , if the key is: 1,2,3,4,5 it should be equal to 9 and display A because 9 is equal to A
because my keys are like this
this.system = [
{
key:0,"value:"x"
},
{
key:1,"value:"x1"
},
{
key:2,"value:"x2"
},
{
key:3,"value:"x3"
},
{
key:4,"value:"x4"
},
{
key:5,"value:"x5"
},
{
key:6,"value:"x6"
},
{
key:7,"value:"x7"
},
{
key:8,"value:"x8"
},
{
key:9,"value:"A"
},
{
key:10,"value:"B"
},
{
key:11,"value:"C"
},
{
key:12,"value:"D"
},
]
CodePudding user response:
If I understood you correctly :
new Vue({
el: '#demo',
data() {
return {
system: [{key:0,value:"x"}, {key:1,value:"x1"}, {key:2,value:"x2" }, {key:3,value:"x3"}, {key:4,value:"x4"}, {key:5,value:"x5"}, {key:6,value:"x6"}, {key:7,value:"x7"}, {key:8,value:"x8"}, {key:9,value:"A"}, {key:10,value:"B"}, {key:11,value:"C"}, {key:12,value:"D"},],
chose: 0,
}
},
computed: {
result() {
return this.system.filter(item => {
if ( this.chose > 0 && this.chose < 6 || this.chose === 9) {
return item.key === 9
} else {
return item.key === this.chose
}
})
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
<input v-model="chose" type="number" />
{{ result }}
</div>
CodePudding user response:
As per my understanding, You want to return value of value
property based on the value of key
property. If Yes, You can simply achieve that by using Array.find()
method. Here you go :
const system = [
{
key:0,value:"x"
},
{
key:1,value:"x1"
},
{
key:2,value:"x2"
},
{
key:3,value:"x3"
},
{
key:4,value:"x4"
},
{
key:5,value:"x5"
},
{
key:6,value:"x6"
},
{
key:7,value:"x7"
},
{
key:8,value:"x8"
},
{
key:9,value:"A"
},
{
key:10,value:"B"
},
{
key:11,value:"C"
},
{
key:12,value:"D"
}
];
const res = system.find(({key}) => key === 9);
console.log(res.value);