J
J
jeruthadam2019-07-04 23:48:03
JavaScript
jeruthadam, 2019-07-04 23:48:03

How to efficiently filter an array within an array?

The question is both about JS and about Vue.
JS first.
There is an array:

[
  ['name', 'surname', 'age', 'job'],
  ['Vasya', 'Pupkin', '22', 'banker'],
  ['Petya', 'Seledkin', '23', 'developer'],
  ['Vova', 'Putin', '88', 'politician'],
  ...еще 10к таких же
]

You need to take out only the necessary indices in each array, for example, 0 and 3:
[
  ['name', 'job'],
  ['Vasya', 'banker'],
  ['Petya', 'developer'],
  ['Vova', 'politician'],
  ...еще 10к таких же
]

It is desirable to do it in the fastest way, because. it happens on the client.
Next question on Vue.js
I tried to filter it with Vue, but it swears:
<table>
      <tr v-for="(item, i) in parsedCSV" :key="i">
        <th v-if="i === 0 || i === 3"  v-for="(rowItem, index) in item" :key="index">
          {{ rowItem }}
        </th>
      </tr>
    </table>

Says you can't v-ifuse v-for. What is the best way to do it then?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-07-05
@jeruthadam

<tr v-for="item in parsedCSV">
  <td v-for="i in [ 0, 3 ]">{{ item[i] }}</td>
</tr>

Or a computed property do:
shortItems() {
  return this.parsedCSV.map(([ name,,,job ]) => [ name, job ]);
},

<tr v-for="item in shortItems">
  <td v-for="el in item">{{ el }}</td>
</tr>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question