0

I have 2 arrays:

var parent = [
  “aaaa”,
  “bbbb|{ba,bb,bc}”,
  ”cccc|{ca,cb,cc|{cca,ccb }}”,
  “dddd”
];  
var child = [
  “eeee”,
  “cccc|{cc|{ccc,ccd},cd}”,
  ”bbbb|{bd}”
];  

I need to merge the elements of these two arrays in such a way that my resulting array is

var result = [
  “aaaa”,
  ”bbbb|{ba,bb,bc,bd}”,
  “cccc|{ca,cb,cc|{cca,ccb,ccc,ccd},cd”,
  ”dddd”,
  ”eeee”
];

The order in which the elements appear in the RESULT array is insignificant. Only requirement is that the grouping of the elements should happen as given in the example. Ie if an element has a ‘|’ operator prefixed by the same string is present in BASE as well as CHILD array, they need to be merged as a single element in the resultant array as shown in the example. The ‘|’ can have a maximum of 2 occurrences in an array element. Can anyone suggest a logic to achieve the same through javascript?

  • If the grouping is relevant, then strings are the wrong data structure to represent this. – Bergi May 13 '14 at 02:38
  • @Bergi—so perhaps the OP should split the strings into a suitable structure, merge them, then stringify again: `myLib.toObject(s0).merge(myLib.toObject(s1)).toArray()`. – RobG May 13 '14 at 03:22

0 Answers0