1

Been wracking my brain and googling like mad, but perhaps I'm not searching with the right keywords.

Got a list/array/JSON serialized data (doesn't really matter at this point) like so:

{ 
  "program": 1,
  "program": 2,
  "program": 3
}

and I'm receiving a series of JSON objects over a node-based socket.io that looks something like this:

{ "program": 1, "sequence_number": 1 }
{ "program": 3, "sequence_number": 2 }
{ "program": 3, "sequence_number": 1 }
{ "program": 2, "sequence_number": 2 }
{ "program": 2, "sequence_number": 1 }
{ "program": 2, "sequence_number": 3 }

And what I am having trouble figuring out is to be able to push/inject/merge this incoming JSON (one at a time as they are received) into my list above to create the following:

{ 
  "program": 1,
    { 
      "sequence_number": 1
    },
  "program": 2,
    {  
      "sequence_number": 2,
      "sequence_number": 1,
      "sequence_number": 3
    },
  "program": 3,
    {
      "sequence_number": 2,
      "sequence_number": 1
    }
}

It's one of those things that is akin to being "on the tip of the tongue" but have reached a wall. So I appeal to the community for some tidbits of wisdom, favored solutions, or pointing in the right direction with any known related tutorials. Thanks in advance!

Update 08.Oct.2016

To address comments regarding the bad format of my list, how about a restructuring of the data, like so:

{
  "program": 1,
  "sequence_list": 
  [
    { "sequence_number": 1 },
    { "sequence_number": 2 }
  ]
},
{
  "program": 2,
  "sequence_list": 
  [
    { "sequence_number": 1 },
    { "sequence_number": 3 },
    { "sequence_number": 2 }
  ]
}
... etc.

Does that make more sense?

the digitalmouse
  • 223
  • 3
  • 25

2 Answers2

3

Try this - similar to what you are asking, but with valid Javascript objects.

var target = { 
  "program1": [],
  "program2": [],
  "program3": []
};

var source = [
  { "program": 1, "sequence_number": 1 },
  { "program": 3, "sequence_number": 2 },
  { "program": 3, "sequence_number": 1 },
  { "program": 2, "sequence_number": 2 },
  { "program": 2, "sequence_number": 1 },
  { "program": 2, "sequence_number": 3 }
];

for (var i=0; i<source.length; i++) {
 item = source[i];
  if (target.hasOwnProperty("program" + item.program)) {
   target["program" + item.program].push(
     {"sequence_number": item.sequence_number}
    );
  }
}

document.getElementById("out").innerHTML = "<pre>" + JSON.stringify(target, null, 2) + "</pre>";
<div id="out">
</div>
Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
  • 1
    Good lad - makes it look pretty ;) – Robin Mackenzie Nov 04 '16 at 11:52
  • 1
    Actually I didn't know the second and third parameters also, thanks! – AMagyar Nov 04 '16 at 11:53
  • Such quick responses! Thanks to all. Yeah I realized I wasn't formatting the data correctly in my question, and I appreciate the reformatting to jog my old brain. Since I will have an unknown number of programs at any time (this is data coming from an external hardware controller) I'll have to concatenate the numbers to the names when building the first list on the fly, but that's no big deal. I'll give Robin's solution a go on Monday when I am back in the office, and upvote/pick a solution then. Thanks for all your input! -jimm – the digitalmouse Nov 05 '16 at 08:22
  • both solutions are appreciated, but I really need to keep the key-value data separated for both the target and the source data.. somehow need to have `"program": 1`, etc. – the digitalmouse Nov 05 '16 at 11:55
  • I am trying to add to class(es): https://stackoverflow.com/questions/45716358/how-to-iterate-data-to-insert-into-a-list-to-create-a-json-format-data. Please help – Si8 Aug 16 '17 at 15:51
1

As for the solution you can change the base:

{ 
  "Program 1": [],
  "Program 2": [],
  "Program 3": []
}

and then you can add the data to the array.

Check this plunker

AMagyar
  • 5,108
  • 3
  • 13
  • 17