0

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.

  • 1
    You current format seems to be rather invalid ? – adeneo Sep 29 '16 at 21:39
  • Adeneo, did you mean the question formatting? If so, it's corrected. If you mean my JS formatting, that's the problem I am having. Thank you. – Shazam Sep 29 '16 at 21:48
  • Well, it says "The current format is this ...", but that's an object without keys containing just objects, so it's invalid and would throw an error if that in fact was the "current format". – adeneo Sep 29 '16 at 22:03
  • Thanks for your clarification. It's actually not throwing any errors in that part of the code, but I see what you are referring to now. I think I intended it to be an array of objects, but clearly based on your comment and the comment about hash (Jamie's comment) vs complex object (my term) vs dictionary list...I am still learning about datastructures and the like. Thanks for your help and time. – Shazam Sep 29 '16 at 22:08
4

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.

| improve this answer | |
  • THANK YOU so much! I knew it had to be something simple ,your explanation makes sense. This works...will accept hte answer as soon as SO lets me (there is a time limit btwn Q&A). Thanks Marc B, you're the best! – Shazam Sep 29 '16 at 21:44
  • for other commenters, this answer fixed my coding question. I'd still appreciate any comments offfered about "better ways" to accomplish my task. – Shazam Sep 29 '16 at 21:50
  • 1
    @Shazam ES 2015 lets you write { [user]: 'blahblahblah' } where the key will be the value of the string user at the time of creation. You can do this today with babel. – Jared Smith Sep 30 '16 at 12:37
0

First of all, the first thing you listed is not an array, it's a hash (also referred to as a dictionary, or map). Second of all, what you want to do is a bad idea. Formatting the key of a key value pair to be your presentation layer is an awful idea. If I were you I would look up the different between arrays and hashes and then if you still decide that you want to create a json with re-formatted key values, use the Array.prototype.map function

| improve this answer | |
  • Jamie, by instinct this was not the way I wanted to code it, but I am using a library called bind.js, and this codepen is the only way I am able to get it to work. Not the 2nd bind object which binds the data to the DOM. There may be many other ways to do what I need (angular 1/2, react...?) but for now I got bind working. I'm open to better ideas for this project, which i'd be happy to elaborate on perhaps as a new question. codepen.io/AdventureBear/pen/qabGXQ – Shazam Sep 29 '16 at 21:46
  • And thank you for the explanation of the hash or dictionary list. I'm not sure what you mean by being in my "presentation layer". Thank you for you rhelp. – Shazam Sep 29 '16 at 21:49
  • it's a hash it's actually an object in JavaScript terms. Using the name "hash" and any of the others is foreign terminology. Except "list" - that is just wrong terminology, since lists contain ordered values, not unordered key-value pairs. – VLAZ Sep 29 '16 at 22:05
  • Also worth noting that as of ES6, the term map becomes confusing if talking about plain objects in JavaScript, since there is now an actual Map added to the API. – VLAZ Sep 29 '16 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.