我已阅读过类似的问题在这里stackoverflow,阅读JavaScript的MDN,并没有找到可比的例子,并且现在问如何创建所需的数组给定的数组。将复杂对象添加到数组中使用foreach或使用javascript映射
所需的数组是以这种格式存在,而不是在每个数组元素处是一个由一个键组成的对象,后跟一个本身就是key:value对的对象的值。 (如果你好奇我为什么要这样编码数组,我需要这种格式才能使用bind.js库)。
所需的格式:
var userSummary = {
ryanc: {
"CreatedBy":"ryanc",
"New Contacts":7,
"New Invites (Challenge Groups)":8,
"New Invites (Coaching Opportunity)":9,
"New Follow Ups":12,
"New Challenger Check-Ins":11,
"Team Call Participation (National, upline team, or our team)":3,
"SCPoints":0,
"Volume":0
},
Pupperpie: {
"CreatedBy":"Pupperpie",
"New Contacts":5,
"New Invites (Challenge Groups)":4,
"New Invites (Coaching Opportunity)":3,
"New Follow Ups":3,
"New Challenger Check-Ins":5,
"Team Call Participation (National, upline team, or our team)":1,
"SCPoints":0,
"Volume":0
},
bowdenke: {
"CreatedBy":"bowdenke",
"New Contacts":14,
"New Invites (Challenge Groups)":3,
"New Invites (Coaching Opportunity)":3,
"New Follow Ups":1,
"New Challenger Check-Ins":0,
"Team Call Participation (National, upline team, or our team)":2,
"SCPoints":0,
"Volume":0
}
};
目前的格式是这样的:
var userSummary = {
{
"Created By":"ryanc",
"New Contacts":7,
"New Invites (Challenge Groups)":8,
"New Invites (Coaching Opportunity)":9,
"New Follow Ups":12,
"New Challenger Check-Ins":11,
"Team Call Participation (National, upline team, or our team)":3,
"SCPoints":0,
"Volume":0
},
{
"Created By":"Pupperpie",
"New Contacts":5,
"New Invites (Challenge Groups)":4,
"New Invites (Coaching Opportunity)":3,
"New Follow Ups":3,
"New Challenger Check-Ins":5,
"Team Call Participation (National, upline team, or our team)":1,
"SCPoints":0,
"Volume":0
},
{
"Created By":"bowdenke",
"New Contacts":14,
"New Invites (Challenge Groups)":3,
"New Invites (Coaching Opportunity)":3,
"New Follow Ups":1,
"New Challenger Check-Ins":0,
"Team Call Participation (National, upline team, or our team)":2,
"SCPoints":0,
"Volume":0
}
};
目前我已经试过这个代码,这将插入一个文本“用户”而不是用户名是这样的:
{"user":{"Created By":"ryanc","New Contacts":0,"New Invites (Challenge Groups)":0,"New Invites (Coaching Opportunity)":0,"New Follow Ups":0,"New Challenger Check-Ins":0,"Team Call Participation (National, upline team, or our team)":0,"SC Points":0,"Volume":0}}
array uniqueUser包含一组用户名,userSummary应该包含新格式特德对象。
我已经尝试了一些其他的这种变化,但似乎无法得到正确的语法。
uniqueUsers.forEach(function(user) {
var obj = { user: {
'Created By': user,
'New Contacts': 0,
'New Invites (Challenge Groups)': 0,
'New Invites (Coaching Opportunity)': 0,
'New Follow Ups': 0,
'New Challenger Check-Ins': 0,
'Team Call Participation (National, upline team, or our team)': 0,
'SC Points': 0,
'Volume': 0
}}
userSummary.push(obj);
})
我希望有任何其他想法,请让我知道,如果需要额外的信息。
您目前的格式似乎相当无效? – adeneo
Adeneo,你的意思是问题的格式?如果是这样,它已被纠正。如果你的意思是我的JS格式,那就是我遇到的问题。谢谢。 – Shazam
好吧,它说*“当前格式是这个......”*,但是这是一个没有包含对象的键的对象,所以它是无效的,如果实际上是“当前格式”则会抛出错误。 – adeneo