Shazam Shazam - 2 months ago 16
Javascript Question

Adding complex objects to an array using foreach or map with javascript

I've reviewed similar questions here on stackoverflow, read the javascript MDN, and not found comparable examples, and am asking now for advice on how to create the desired array from the given array.

The desired array is in this format, not at each array element is an object consisting of one key followed by a value that is itself an object of key:value pairs. (if you are curious why I'm coding the array like this, I need this format in order to use the bind.js library).

Desired format:

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


The current format is this:

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


Currently I have tried this code, which inserts a literal 'user' instead of the users name like this:

{"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}}


The array uniqueUser contains an array of usernames, userSummary should contain the newly formatted object.

I have tried a few other variations of this as well but cannot seem to get the syntax correct.

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


I would appreciate any additional ideas, please let me know if additional information is needed.

Answer

Problem is this:

var obj = { user:  {
              ^^

Since JS doesn't require quotes for object keys, there's no way to use a variable for this, because you can't tell between "user-is-a-key" and "user-is-a-variable".

You'd have to do

var obj =  {};
obj[user] = {...}

instead, where the parser will KNOW that user is a variable, and not a quoteless-string.