A
A
Andrej Sharapov2020-02-25 17:40:56
Vue.js
Andrej Sharapov, 2020-02-25 17:40:56

Split array into vuejs arrays?

Hi all!
Tell me how to split one array into different arrays in vue, provided:

1. the array can consist of n-number of objects;
2. objects in the array do not have cyclicity;
3. each object has a bunch of data and type_id , which can be either 0 or 1 ; Based on this condition, there should be two arrays.

list: [{..., ..., type_id: 0, ..., ..., }, {..., ..., type_id: 0, ..., ..., }, {..., ..., type_id: 0, ..., ..., }, {..., ..., type_id: 1,..., ..., }, ..., ..., ]


It is possible somehow through Lodash > Filter, but I did not use it. I try natively through computed.
Each array will need to be displayed in different places on the page, preferably in one component, for example, <my-component тут условия, если тип_ид === 0 />. I don't know how best to do it. Tell me how to solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2020-02-25
@Madeas

If the two new arrays are intended to be used independently, then two computed properties must be created. And then a simple filter to sort through all the elements:

{
  computed: {
    id1() {
      return this.list.filter(i => i.type_id === 1)
    },

    id0() {
      return this.list.filter(i => i.type_id === 0)
    }
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question