0

I need to use multidimensional array model in my vuejs project

Fiddle of my code:

I define an array in my vuejs data

new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!',
    arr: [[1,2],[3,4]]
  }
})

and use:

<p v-if="arr[3][0]">{{ arr[3][0] }} //Vue warn]: Error in render function: "TypeError: Cannot read property '1' of undefined"
</p>

but I have error when I call not defined index

Vue warn]: Error in render function: "TypeError: Cannot read property '1' of undefined"

I use v-if to skip undefined index but it doesn't work Fiddle

How can I solve this problem and skip not defined index?

UPDATE:

v-if not work correctly because of using it in <br> tag I changed <br> to <span> and problem solved

2
  • 1
    Array indexes start at 0, try arr[1][0] Commented Apr 29, 2017 at 12:43
  • @taylorc93 I know that , I want to skip error for not defined index Commented Apr 29, 2017 at 12:44

1 Answer 1

2

You need to test whether the top-level array element exists before testing whether the second-level array element exists.

<p v-if="arr[2] && arr[2][0]">
Sign up to request clarification or add additional context in comments.

2 Comments

I did check it on the fiddle, and it works fine. jsfiddle.net/50wL7mdz/30599
my problem was because of using if condition in br tag I replaced with span and solved

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.