javascript - For loop only shows last value of array - Stack Overflow

So I've got an object called "options" with these items in it:var options = {assignment

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

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

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 9, 2018 at 12:35 BlueWillBlueWill 291 gold badge1 silver badge3 bronze badges 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. – epascarello 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? – Pointy 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. – BeingExpert Commented Jul 9, 2018 at 12:38
  • 2 what is your assignment variable supposed to look like after the loop? – T A 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. – Adam Jenkins Commented Jul 9, 2018 at 12:44
 |  Show 5 more ments

4 Answers 4

Reset to default 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 :

As you can see, my assignment is changing

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

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)
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);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743782500a4506045.html

相关推荐

  • javascript - For loop only shows last value of array - Stack Overflow

    So I've got an object called "options" with these items in it:var options = {assignment

    7天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信