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?

6
  • 1
    { "key" : "value", { } } isn't a valid json Commented Nov 4, 2016 at 11:36
  • true! i'm trying to go for some type of subkey-value pairing that relates the incoming JSON data to existing Program values - hence the question. Commented Nov 4, 2016 at 11:43
  • 1
    {"program": 1, "program": 2, "program": 3} isn't valid either - duplicate keys. Commented Nov 4, 2016 at 11:46
  • can you change the first set to {"program" : [1,2,3] } , as it is not valid json object, it will be change to {"program":3} at the end of the interpretation Commented Nov 4, 2016 at 11:47
  • @A.T. I suppose I could, as long as I get the "program": 3 or program: 3 layout with the sequence numbers underneath matched up. Commented Nov 5, 2016 at 11:58

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

5 Comments

Good lad - makes it look pretty ;)
Actually I didn't know the second and third parameters also, thanks!
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
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.
I am trying to add to class(es): stackoverflow.com/questions/45716358/…. Please help
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.