grouping of similar array items under a parent based on a condition javascript -


var texttag = [{text:"3", items:[{name:item1.jpeg}, {name:item2.jpeg}, {name:item3.jpeg}]},                 {text:"2", items:[{name:item1.gif}, {name:item2.gif}, {name:item3.png}]}, ] 

i trying result array this:

text:3 |_ jpeg     |_item1     |_item2     |_item3  text:2     |_ gif         |_item1         |_item2      |_ png         |_item3  

but have written code gives me following output.

text:3     |_ jpeg         |_item1     |_ jpeg         |_item2     |_ jpeg         |_item3  text:2     |_ gif         |_item1     |_ gif         |_item2     |_ png         |_item3 

please me desired result

you need grouping it.

var texttag = [{ text: "3", items: [{ name: 'item1.jpeg' }, { name: 'item2.jpeg' }, { name: 'item3.jpeg' }] }, { text: "2", items: [{ name: 'item1.gif' }, { name: 'item2.gif' }, { name: 'item3.png' }] }],      grouped = texttag.map(function (a) {          var o = {};          a.items.foreach(function (b) {              var n = b.name.split('.');              o[n[1]] = o[n[1]] || [];              o[n[1]].push(n[0]);          });          return o;      });    document.write('<pre>' + json.stringify(grouped, 0, 4) + '</pre>');


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -