1

I want to use a JS array in Vue.js function for looping.

Tried code:

Vue:

computed: {
 bbb: function(){
  var vvv=this.Preused.length-this.quote.lines.length;
  let added_products=[];
  if(vvv > 0){
    for(var i = 0; i <= vvv-1; i++){
      newly_used.push({...this.Preused[this.manage.code.length+i]});
    }
  }
  return newly_used;
 }
}

HTML:

<div v-for="(cc,index) in bbb">
  <p v-text="cc[index].num"></p>
</div>

newly_used:

0:
  none: "calibry"
  des: "Silver"
  num: "numty"

By using the above code I am getting the following error;

[Vue warn]: Error in render: "TypeError: Cannot read property 'num' of undefined"

How Can I solve this?

0

2 Answers 2

1

cc.num may work, cc is single item in bbb, and you shoule check if cc exist

Sign up to request clarification or add additional context in comments.

Comments

1

You're looping through newly_used properties which is returned from computed property so your v-for should be like :

<div v-for="(cc,index) in bbb" :key="index">
  <p v-text="cc.num"></p>
</div>

or

<div v-for="(cc,key,index) in bbb" :key="index">
  <p v-text="bbb[key].num"></p>
</div>

Comments

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.