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

Community
  • 1
  • 1
BlueWill
  • 29
  • 1
  • 1
  • 3
  • 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. – epascarello Jul 09 '18 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? – Pointy Jul 09 '18 at 12:37
  • 2
    @BlueWill : First of all It should be `options.assignments` and not `options.data.assignments`, assuming your snippet is correct. – BeingExpert Jul 09 '18 at 12:38
  • 1
    what is your assignment variable supposed to look like after the loop? – T A Jul 09 '18 at 12:39
  • is this merely typo or you didnt know the difference between `=` and `==` and `===`? – Isaac Jul 09 '18 at 12:41
  • @BeingExpert it's normal, I voluntarily simplified my object for my question. It is indeed part of an other object called Data. – BlueWill Jul 09 '18 at 12:43
  • @BlueWill - nobody knows what you want `options.data.assignment` to actually be/contain after the loop. Please clear that up. – Adam Jenkins Jul 09 '18 at 12:44
  • he asked why the data aren't the ones he needs, not how he would like the data to be – Hearner Jul 09 '18 at 12:44
  • 1
    is `otherFunction` asynchronous by any chance? – Keith Jul 09 '18 at 12:49
  • If you make assumptions about what code is important and what code isn't, your assumptions may be wrong and you're just wasting people's time (and not getting the answer you need). – Pointy Jul 09 '18 at 12:59

4 Answers4

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

Hearner
  • 2,711
  • 3
  • 17
  • 34
  • The thing is, I need each one of the values because I'm using the options object as a parameter when I call another function inside my loop. See the edits I did on my post. Thanks for your help. – BlueWill Jul 09 '18 at 12:48
  • @BlueWill your question is `Any ideas what's the problem here ?` I answer that, if you want something else, edit your post so people will understand what you really need :) I think that what you want is to append the values into an array – Hearner Jul 09 '18 at 12:49
  • I know it's quite unclear, I've made some edits to my post to explain in more details what I'm trying to do. I'm aware that at the end of the loop the last value of the array will be set in the assignment var. What I don't understand, is why it's set to the last value of my array enven in the middle of the loop when "i" = 0 ou "i" = 1. – BlueWill Jul 09 '18 at 13:02
  • We need further explanation because in your example we cannot know what is wrong. Because, yes, in the middle of the loop `otherFunction(options);` will return different value – Hearner Jul 09 '18 at 13:07
  • Ok so I added what the consol.log are showing me during the loop and what I was expecting to see. – BlueWill Jul 09 '18 at 13:25
  • @BlueWill I also edited my answer. Is it something like this you want ? (Just run code snippet) – Hearner Jul 09 '18 at 13:30
  • 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. – BlueWill Jul 09 '18 at 13:45
  • Not for me, there is something you do that you did not tell us (@BlueWill i edited) – Hearner Jul 09 '18 at 14:13
  • Then I don't know. Because I can't reproduce your error i can't help you more than i already did – Hearner Jul 09 '18 at 14:36
  • It's ok, I will look deeper in the code and find a solution eventually. Thanks for your time ! – BlueWill Jul 09 '18 at 14:41
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(',')
MKougiouris
  • 2,821
  • 1
  • 16
  • 19
  • 1
    or even easier `options.data.assignment = options.data.assignments.join(',')` this will also leave of the last `,` – phuzi Jul 09 '18 at 12:43
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)
Manoj
  • 1,175
  • 7
  • 11
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);
Ankit
  • 51
  • 3
  • or even easier `options.data.assignment = options.data.assignments.join(';')` this will also leave of the last `;` – phuzi Jul 09 '18 at 12:44
  • Yes. All I wanted to point out was that, operator needed to be updated for getting the desired result. – Ankit Jul 09 '18 at 12:47
  • 1
    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. – phuzi Jul 09 '18 at 12:48