最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to increment a value inside an object variable - Stack Overflow

programmeradmin5浏览0评论

I have the following.

var dataset = {val1 : 0, val2 : 0, val3 : 0};
var person = [];
var totalPeople = 10;

for(var i = 0; i <totalPeople; i++) {
   person[i] = dataset; 
}

Why i chose this approach, click here .

I'm trying to make one of the values auto increment inside another for loop.

I've tried the following approaches to no avail.

person[1]{val1 : 0,
          val2 : 0,
          val3 : val3 + 1};

person[1]{val1 : 0,
          val2 : 0,
          val3 : person[1].val3 + 1};

person[1].val3 = person[1].val3 + 1;

any ideas?

I have the following.

var dataset = {val1 : 0, val2 : 0, val3 : 0};
var person = [];
var totalPeople = 10;

for(var i = 0; i <totalPeople; i++) {
   person[i] = dataset; 
}

Why i chose this approach, click here .

I'm trying to make one of the values auto increment inside another for loop.

I've tried the following approaches to no avail.

person[1]{val1 : 0,
          val2 : 0,
          val3 : val3 + 1};

person[1]{val1 : 0,
          val2 : 0,
          val3 : person[1].val3 + 1};

person[1].val3 = person[1].val3 + 1;

any ideas?

Share Improve this question edited May 23, 2017 at 12:18 CommunityBot 11 silver badge asked Sep 16, 2014 at 19:34 SwiftSwift 611 gold badge5 silver badges8 bronze badges 4
  • 1 person[i].val3+=1 ? Not sure what you want to do – juvian Commented Sep 16, 2014 at 19:37
  • 2 How it is connected with jquery? – u_mulder Commented Sep 16, 2014 at 19:39
  • Are you trying to increment the field name, or the field value? – Josiah Ruddell Commented Sep 16, 2014 at 19:40
  • Juvian, I'll expand the code a little. U_mulder, I was writing with the intention of Jquery.. but this was an answer given to another question. oops if it is pure js [newby :) ] – Swift Commented Sep 16, 2014 at 19:43
Add a ment  | 

3 Answers 3

Reset to default 7

This should be the right:

person[1].val3 += 1;

This should work.

var dataset = {val1 : 0, val2 : 0, val3 : 0};
var person = [];
var totalPeople = 10;

for(var i = 0; i <totalPeople; i++) {
   dataset[i].val3 ++; 
}

Could you explain more what you are trying to achieve?

Totally sorry. The solution that you're referring to here was posted by me and is incorrect. I just updated my answer in that post.


Don't use the array initialization style that I originally posted:

var dataset = {val1 : 0, val2 : 0, val3 : 0};
var person = [];
var totalPeople = 10;

for(var i = 0; i < totalPeople; i++) {
  person[i] = dataset; // this assigns the *same* object reference to every 
                       // member of the person array.

}


This is the correct way to initialize your person array:

var person = [];
var totalPeople = 10;

for(var i = 0; i < totalPeople; i++) {
  person[i] = {val1 : 0, val2 : 0, val3 : 0}; // do this to create a *unique* object 
                                              // for every person array element
}


If you use the correct array initializtion shown directly above, then you can increment val3 like this with each loop iteration:

var person = [];
var totalPeople = 10;

for(var i = 0; i < totalPeople; i++) {
  person[i] = {val1 : 0, val2 : 0, val3 : 0};
  person[i]['val3'] = i;
}


Sorry again for the bad information that I provided in the other post. (All other info is correct. Just the array initialization code was bad.) I hope this updated information helps.

发布评论

评论列表(0)

  1. 暂无评论