Reputation: 29
So I've got an object called "options" with these items in it:
var options = {
assignments: ['involved', 'assignee', 'candidate'],
assignment: ""
}
With a for loop, I'm trying to put the values from my array assignments in the assignment var, one by one. Then I'm using "options" with the new value as parameters for another function I'm calling inside my loop.
for (var i = 0; i < options.data.assignments.length; i++) {
options.data.assignment = options.data.assignments[i];
console.log("value of i : ",i);
console.log("value of options :",options);
otherFunction(options);
}
I was expecting to see results like this :
value of i : 0
value of options : {assignment = "involved"...}
value of i : 1
value of options : {assignment = "assignee"...}
value of i : 2
value of options : {assignment = "candidate"...}
But instead I have something like this :
value of i : 0
value of options : {assignment = "candidate"...}
value of i : 1
value of options : {assignment = "candidate"...}
value of i : 2
value of options : {assignment = "candidate"...}
The thing is while doing that my assignment variable is always set to "candidate", the value at the end of my array.
The strange thing is when I'm trying to console.log(options.data.assignments[i])
, the right value shows up. Same for the "i", it goes from 0 to 1 then 2 and stops properly. So my loop is working perfectly fine, except when I want to set the value of my variable.
Any ideas what's the problem here?
Thanks
Upvotes: 1
Views: 12550
Reputation: 2729
for (var i = 0; i < options.data.assignments.length; i++) {
options.data.assignment = options.data.assignments[i];
}
will loop 3 times:
options.data.assignment = options.data.assignments[0]
-> options.data.assignment='involved'
options.data.assignment = options.data.assignments[1]
-> options.data.assignment='assignee'
options.data.assignment = options.data.assignments[2]
-> options.data.assignment='candidate'
So in fact, you are assigning 3 different data one by one to the same value, so, in the end options.data.assignment
will be the last value of your loop.
Here is what you do with a easier example :
var a = 0;
for (var i = 0; i < 3; i++) {
a = i;
}
as you can see, in the end the variable a
will always be the last value of the loop i
.
First of all It should be options.assignments
and not options.data.assignments
.
Here is something you want
var options = {
assignments: ['involved', 'assignee', 'candidate'],
assignment: ""
}
for (var i = 0; i < options.assignments.length; i++) {
options.assignment = options.assignments[i];
console.log("value of i : ",i);
console.log("value of options :", options);
// otherFunction(options);
}
As you can see, my assignment is changing
Upvotes: 6
Reputation: 51
var options = {
assignments: ['involved', 'assignee', 'candidate'],
assignment: ""
}
for (var i = 0; i < options.assignments.length; i++) {
options.assignment += options.assignments[i]+";"; //updated the assignment operator to get desired result
}
console.log(options.assignment);
Upvotes: 0
Reputation: 2861
You are assigning to the same var everything, replacing the previous values as you go along. If you just want to concatenate all elements from the array to the string you can do this
for (var i = 0; i < options.data.assignments.length; i++) {
options.data.assignment += options.data.assignments[i]+',';
}
EDIT: as said by @puzhi : You can also do this to take care of the last ',' without removing it after
options.data.assignment = options.data.assignments.join(',')
Upvotes: 3
Reputation: 1195
you are reassigning same variable with each iteration in loop thats why last value gets assigned after third loop.
You can simply do this to achieve what you need.
options.data.assignment = options.data.assignments.map(ele=>ele)
Upvotes: 0