2

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"
 },
]
3
  • What's your expected output?
    – James
    Commented Sep 20, 2022 at 18:55
  • i alrady stated in my question
    – Noah
    Commented Sep 20, 2022 at 19:14
  • This statement is confusing: if the key is: 1,2,3,4,5 it should be equal to 9. The entries for those keys contain a value that is not equal to 9 (for instance, the entry for key 1 is x1), so why do you expect a value of 9?
    – tony19
    Commented Sep 21, 2022 at 3:19

2 Answers 2

0

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>

4
  • not all will return A, i mean that if the key selected is from 1 to 5, they will return me the value of 9 which is A, if they choose anything like key:10, the value of 11 will be returned which is C @nikola
    – Noah
    Commented Sep 20, 2022 at 20:38
  • @Noah hey mate, check again pls,did I understood now? (just change chose variable and look result) Commented Sep 20, 2022 at 21:05
  • Thanks but i also need to hide those values to show in a dropdown where i a using <el-option v-for="(item, key) in source" :key="key" :label="item.value" :value="item.key" >
    – Noah
    Commented Sep 21, 2022 at 11:32
  • please see above comments
    – Noah
    Commented Sep 21, 2022 at 12:38
0

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);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.