0

I have an array in my Vue component that I'm trying to parse out in a method, but I don't want the ID in it at all. I want the array to be the same as it is currently but only show the option index, not the id.

Is there a way, within the method, that I can pop the ID index out of the array entirely?

var vm = 
new Vue({
  el: "#app",
  data: {
    options: [
      {id:100, option:"1 - John Doe"},
      {id:200, option:"2 - Jane Doe"}
    ]
  },
  methods: {
    getNames(){
      
      let names = this.options;
      
      console.log(names);
    }
  }
  
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button @click="getNames()">TEST</button>
</div>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Geoff_S
  • 4,917
  • 7
  • 43
  • 133
  • Where do you consume the condensed options or what is the planed purpose for `getNames`? Asking because Vue.js might have a simpler solution to achieve rendering, filtering, templating, etc. (usually you keep original data in model and use what you need in the view (benefit and strength of MVC / MVVM). – hc_dev Feb 02 '22 at 23:12
  • Although requiring "but I don't want the ID in it at all", you [accepted an answer](https://stackoverflow.com/a/70963354/5730279) which has the ID property remapped as`id: option.option`. Please clarify your question (give an example of wanted output). – hc_dev Feb 03 '22 at 06:14

3 Answers3

1

You can use map method:

var vm = 
new Vue({
  el: "#app",
  data: {
    options: [
      {id:100, option:"1 - John Doe"},
      {id:200, option:"2 - Jane Doe"}
    ]
  },
  methods: {
    getNames(){
      
      let names = this.options.map(o => o.option);
      
      console.log(names);
    }
  }
  
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button @click="getNames()">TEST</button>
</div>
Nikola Pavicevic
  • 21,952
  • 9
  • 25
  • 46
1

You can loop through the array and create another array

var vm = 
new Vue({
  el: "#app",
  data: {
    options: [
      {id:100, option:"1 - John Doe"},
      {id:200, option:"2 - Jane Doe"}
    ]
  },
  methods: {
    getNames(){
      
      let names = this.options.map(option => {
        return { 
          option: option.option
        }
      });
      
      console.log(names);
    }
  }
  
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<button @click="getNames()">TEST</button>
</div>
Rotiken Gisa
  • 380
  • 2
  • 12
1

This is a pure JavaScript task. It is solvable independently from VueJS or any other framework.

In general you can apply two approaches (either keep what is needed or remove what is obsolete):

  1. map: to transform each source element to the new target format; returns a new array
  2. forEach with delete: to remove the unwanted property from the original array
let options: [
  {id:100, option:"1 - John Doe"},
  {id:200, option:"2 - Jane Doe"}
];

// to get a new (simple) array of strings
let strings = options.map(o => o.option);
console.log(strings);

// to get the same array with nested objects where property was removed
options.forEach(o => delete o.id);
console.log(options);

See also: Remove property for all objects in array

hc_dev
  • 8,389
  • 1
  • 26
  • 38