【问题标题】:I have an array of values with a name connected to each one, I can post the values with scores.toString(); but how do I post the names along with it?我有一个值数组,每个值都有一个名称,我可以使用 score.toString(); 发布值但是我如何将名称与它一起发布?
【发布时间】:2020-07-04 09:01:51
【问题描述】:
function calc_results(){
    var scores = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    for (var StatementCounter = 0; StatementCounter < 29; StatementCounter++){ // 29 statements it loops trough
        for (var positionCheck = 0; positionCheck < 23; positionCheck++){ // 23 parties it loops through
            if(choices[StatementCounter] == subjects[StatementCounter].parties[positionCheck].position){ // if my choice is the same as the party's choice
                var partyposition = getpartyposition(subjects[StatementCounter].parties[positionCheck].name);
                scores[partyposition]++;
            }
        }
    }
}

function getpartyposition(partyname){
    for (var positionCheck = 0; positionCheck < 23; positionCheck++){
        if(parties[positionCheck].name == partyname){
            return positionCheck;
        }
    }
}

// these are the parties that are already connected to the string, there are actually 23 of them.
var parties = [{
    name: "VVD",
  },
  {
    name: "CDA",
  },
  {
    name: "PVV",
  }

如果我共享的代码不足,以下是链接: (其中一些是荷兰语,但任何荷兰语对于代码的完整性都不重要,因此可以忽略) https://github.com/ChancellorXeno/BackAtItAgain/blob/master/choice.js https://github.com/ChancellorXeno/BackAtItAgain/blob/master/data_array.js(各方意见决定其在scores数组中的值)

【问题讨论】:

  • 你能添加一些小数据集和它想要的结果吗?

标签: javascript arrays loops multidimensional-array


【解决方案1】:

Scores 是一个 int 数组,因此只需执行 score[0].toString 即可。

但是party 是一个对象数组。你需要做这样的事情,parties[0].name。不需要做 toString 因为它已经是一个字符串了。

【讨论】:

  • 它并没有直接解决我的问题,但它确实引导我找到了解决方案,所以谢谢!
猜你喜欢
  • 2016-01-20
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
相关资源
最近更新 更多