0

If I set to Vue.data array like this [0: Object, 2: Object]; I will get in Vue console log panel array like this [0: Object, 1: undefined 2: Object]; and after iteration in 'v-for="cell in row.cells"' I got the problem of getting any property of undefined.

I resolved my problem like this:

v-for="cell in row.cells" v-bind:colspan="cell.colspan" v-if="typeof cell !== 'undefined'"



v-for="cell in row.cells" v-bind:colspan="cell.colspan" v-if="typeof cell !== 'undefined'"

I want to get in Vue array as I've tried to set without index shift or any array changings.

2 Answers 2

3

Vue can iterate Objects the same as they do Arrays, if you insist on named values why not do this:

cells: {
  0: {},
  1: {},
  2: {}
}

If you got unwanted "data" in your array, you could say the following.

const filteredCells = row.cells.filter(cell => cell !== undefined)

and then iterate the filteredCells instead, id make filteredCells a computed value.

v-for="cell in filteredCells"

Keep index:

export default {
  data() {
    return {
      row: {
        cells: [
          {name: 'peter'}, 
          {name: 'louise'}, 
          {name: 'hans'}, 
          undefined, 
          {name: 'mia'}, 
          {name: 'john'}
        ]
      }
    }
  },

  computed: {
    filteredCellsWithIndex() {
      if (!this.row || !this.row.cells) {
        return
      }

      const { cells } = this.row

      return cells.map((cell, index) => {
        if (cell === undefined) {
          return
        }

        cell.yourIndex = index
        return cell
       }).filter(cell => cell !== undefined)
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

But after filter array you will kill indexes [0 => 1, 1 => undefined , 2 => 2] => [0 => 1, 1 => 2] I want to save my indexes
Run through the array, add the index to the object, then chain filter() afterwards, to remove undefined, and then delete your v-if on iteration.
0

There are a couple of ways to cleanly remove your undefined values without making your template overly complicated.

Create a filter.

v-for="cell in row.cells | isDefined"
{
  filters: {
    isDefined(items) {
      return items.filter(it => it !== undefined)
    }
  }
}

Or use a computed property.

v-for="cell in cells"
{
  computed: {
    cells() {
      return this.row.cells.filter(it => it !== undefined)
    }
  }
}

3 Comments

The wish to keep index values for each property as they were in the original array, doesen't get solved with this.
If OP NEEDED index values, the template would use for (cell,index) in ... OP can simply use indexOf(cell) to get index if needed.
No, ['a', 'b', undefined, 'd'], he still wants 'd' to be of index 3, your solution (as I suggested as well at first) will make 'd' using (cell, index) to index 2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.