如何在javascript中的单个变量中分配多个值?

时间:2016-03-30 11:58:54

标签: javascript arrays angularjs

我有一个插入团队ID的数组,

var responsiblePartyId = [];
for (var i in addTeamtask.toteam) {
    responsiblePartyId.push(addTeamtask.toteam[i].SiteUserId);
}

我只需从上面的数组中获取值并存储到单个变量中。 我在下面尝试过,但是我分配了数组的最后一个值。

var assignedID;
for (var i = 0; i < responsiblePartyId.length; i++) {
    assignedID= '"' + i, + '"';
}

1 个答案:

答案 0 :(得分:4)

var assignedID = responsiblePartyId.join(','); // To join all entries with a comma in between
         // If you want them joined using a different character(s), modify accordingly

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join