-1

I am trying to merge the properties of an array into JSON-like string as a new property.

Given something like:

[{
   {
     "orderNo":"1",
     "description":"Item 1",
     "note": "Note 1"
   },
   {
     "orderNo":"2",
     "description":"Item 2",
     "note": "Note 2"
   },
   {
     "orderNo":"2",
     "description":"Item 3",
     "note": "Note 3"
   }
}]

And converting it into:

[{
   '0':[
      {
         'orderNo':'1',
         'items': '[{"description":"Item 1", "note": "Note 1"}]'
      }
   ],
   '1':[
      {
         'orderNo':'2',
         'items': '[{"description":"Item 2", "note": "Note 2"}, {"description":"Item 3", "note": "Note 3"}]'
      }
   ]
}]

Using the following function (supplied by @Barmar) I can accumulate a single property into an items array (in this case, itemId).

var newData = [];
for (var i = 0; i < data.length; i++) {
  var orderNo = data[i].orderNo;
  if (!newData[orderNo]) { // Add new object to result
    newData[orderNo] = {
      orderNo: orderNo,
      items: []
    };
  }
  newData[orderNo].items.push('{"itemId":' + data[i].itemId + ',"description:"' + data[i].description); // how than this be converted into a string?
}

How can I string multiple properties together and maintain their association so that they can be later parsed with JSON.parse?

Matthew
  • 1,461
  • 3
  • 23
  • 49
  • 2
    This is unclear because there is not a pattern. Why `"description":"Item 3"`? – Ele Mar 22 '18 at 15:46
  • 2
    Why is there a single object with "numeric" keys starting with zero wrap in an array? O.o – Andreas Mar 22 '18 at 15:50
  • it dosent have a consistent pattern – Abslen Char Mar 22 '18 at 15:52
  • Yes, there was a typo. It is now corrected in the question. – Matthew Mar 22 '18 at 16:10
  • @Andreas This is pattern was made to group order items by order number from a relational database. – Matthew Mar 22 '18 at 16:20
  • Why does `'description"` have non-matching quotes? Why do you want a "JSON-like" string instead of real JSON? – Barmar Mar 22 '18 at 16:30
  • @Barmar sorry, typo. – Matthew Mar 22 '18 at 16:31
  • Why do you want a JSON string in the first place? Why not just an ordinary array. JSON should only be used when serializing for transmission or storage. – Barmar Mar 22 '18 at 16:31
  • @Barmar There are limitations in the front-end to iterating over data. So it needs to be stringified and then parsed as it gets passed through the DOM tree. – Matthew Mar 22 '18 at 16:32
  • @Barmar See https://stackoverflow.com/questions/49430244/how-to-structure-associative-array-to-inject-into-dom-repeat – Matthew Mar 22 '18 at 16:33
  • So stringify it when you put it into the DOM tree, not in the object. – Barmar Mar 22 '18 at 16:33
  • Anyway, you still haven't explained why you need JSON-like instead of JSON. That will be difficult, because you'll need to write your own stringifier from scratch. – Barmar Mar 22 '18 at 16:34
  • @Barmar I'm not seeing a way to do that within the Polymer framework. – Matthew Mar 22 '18 at 16:34
  • @Barmar Yes, a JSON string. It was a poor word choice. – Matthew Mar 22 '18 at 16:34
  • Your example isn't JSON. JSON uses double quotes, not single quotes, around all strings. – Barmar Mar 22 '18 at 16:35
  • @Barmar OK... not sure how to properly format it so there is a JSON string as a value in an array... – Matthew Mar 22 '18 at 16:36
  • See https://stackoverflow.com/questions/24302630/how-combine-the-array-in-javascript/24302781#24302781 for how to regroup your object using the `time` property. Then you just need to call `JSON.stringify()` on the `items` property in the resulting object. – Barmar Mar 22 '18 at 16:36
  • `object.items = JSON.stringify(object.items)` – Barmar Mar 22 '18 at 16:37
  • @Barmar I have the base function ordering by a desired property, but am unsure where to implement tho stringify function and how to push multiple properties and keep their association. – Matthew Mar 22 '18 at 18:43
  • @Matthew Did you look at the question I linked to for how to push properties and keep their association? – Barmar Mar 22 '18 at 18:44

1 Answers1

1

One of the possible way is below. I use Array.prototype.forEach.

x1=[{0:[{time:"2018-02-20",description:"Item 1",note:"Note 1"}],1:[{time:"2018-02-21",description:"Item 2",note:"Note 2"},{time:"2018-02-21",description:"Item 3",note:"Note 3"}]}];
var y1={};

x1.forEach(function(x){  // iterate through array of object
  var index=0;
  for(var a in x){       // iterate through all keys of object
    var y={};
    x[a].forEach(function(e){ //iterate through inner array
      y["time"]=e.time;       //crate new object of desire value
      if(!y["items"]){
        y["items"]=[];
      }
      y["items"].push({'description':e["description"],'note':e["note"]});     //push object into items property of object
    });
    y1[Object.keys(x[a])[index++]]=y; // create final object property and value
  }
});

console.log(y1);
yajiv
  • 2,901
  • 2
  • 15
  • 25