1

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

10
  • 1
    options.data.assignment = options.data.assignments[i]; makes no sense.... you are reassigning what you are looping over? It is going to be the last value because the loop ends at the last value. Commented Jul 9, 2018 at 12:37
  • 3
    When you assign to a variable, you replace the old value with the new value. What is it that you expect to happen? Commented Jul 9, 2018 at 12:37
  • 2
    @BlueWill : First of all It should be options.assignments and not options.data.assignments, assuming your snippet is correct. Commented Jul 9, 2018 at 12:38
  • 2
    what is your assignment variable supposed to look like after the loop? Commented Jul 9, 2018 at 12:39
  • 1
    @BlueWill - nobody knows what you want options.data.assignment to actually be/contain after the loop. Please clear that up. Commented Jul 9, 2018 at 12:44

4 Answers 4

6
for (var i = 0; i < options.data.assignments.length; i++) {
  options.data.assignment = options.data.assignments[i];
}

will loop 3 times:

  1. options.data.assignment = options.data.assignments[0] -> options.data.assignment='involved'
  2. options.data.assignment = options.data.assignments[1] -> options.data.assignment='assignee'
  3. 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);
}

Here is what i get : enter image description here

As you can see, my assignment is changing

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

4 Comments

This is what I would like yes, when I do the console.log(options.assignment) I do get the same results as you, but when I do console.log(options) then assignment is set to "candidate" for all three iteration.
Not for me, there is something you do that you did not tell us (@BlueWill i edited)
Then I don't know. Because I can't reproduce your error i can't help you more than i already did
It's ok, I will look deeper in the code and find a solution eventually. Thanks for your time !
3

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(',')

1 Comment

or even easier options.data.assignment = options.data.assignments.join(',') this will also leave of the last ,
0

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)

2 Comments

I don't think that's what the OP actually wanted to do
To be fair, it's completely unclear what the OP is trying to do, but it also doesn't make sense to guess at what they want and post your guess as an answer.
0
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);

3 Comments

or even easier options.data.assignment = options.data.assignments.join(';') this will also leave of the last ;
Yes. All I wanted to point out was that, operator needed to be updated for getting the desired result.
Should have probably explained that in your answer rather than just a block of code. Can be difficult to spot the changes unless you know what you're looking for.

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.