0

I am working on a little exercise with the Javascript Vue.js Framework. I cut the following code to show my problem. I have two variables with no value in the data part. I try to edit the two variables in a method.

I stored two number values in a array called "counter".

I stored the two variables (a and b) from the data part in an array called "values" (with the "this") before.

Then I use two for loops to edit them. But I can't get access to the variables a and b, when they are stored in the array "values". I think my problem is the Array "values". If i change the values[i] in the last for-loop in "this.a", it works. But not if I want to get access with the variables stored in the array.

I think it is a JavaScript problem but I don't know, how I can solve it. I already looked up some similar questions in stack Overflow but I can't transfer the solutions to my case.

// Vue.js Model-Part

data: {
   a = '';
   b = '';
}

//A snippet out of the method-part
var one = 13;
var two = 14;

counter = [one, two];
values = [this.a, this.b];

for(var i = 0; i < counter.length-1; i ++){
    for(var position = 0; position < counter[i]; position ++){
        values[i] += "*";
};

4
  • 1
    Hello @LDP97 could you be more cleared to explain what you want to acheive with vuejs. Pls Commented May 17, 2020 at 22:06
  • I don't think you are accessing the data object using this keyword. When you console.log this.a, what do you get? Commented May 17, 2020 at 22:10
  • @Birante I have to built a little rich-client. I know my example is not that good. I tried to show my problem very quick. I am sorry if my code is not understandable. Commented May 17, 2020 at 22:17
  • @Syntiara My original code looks not like my given example. But with the this keyword I get the access to the variable in the data part. Commented May 17, 2020 at 22:19

1 Answer 1

1

Because a and b are strings their values copied in the values array. So doing

for(var position = 0; position < counter[i]; position ++){
        values[i] += "*";

just changing copies of their values in the values array. So you either should modify this.a and this.b directly or you can capture these variable into a function that changes their values directly itself.

var functionToChangeAandB = (val1, val2) => {
  this.a = val1
  this.b = val2
}

functionToChangeAandB('one', 'two')
Sign up to request clarification or add additional context in comments.

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.