1

I have an object with structure of:

[ 
  {"vid":"aaa", "san":12}, 
  {"vid":"aaa", "san":18}, 
  {"vid":"aaa", "san":2}, 
  {"vid":"bbb", "san":33}, 
  {"vid":"bbb", "san":44}, 
  {"vid":"aaa", "san":100}
]

This object is passed to function called SerializeData(inData) as inData variable:

function SerializeResults( inData ) {
    $.each( inData, function( i, val ) { ... })
} 

I want to iterate all objects in inData and transform them, so the result will be like this:

[ 
  { "vid":"aaa","san":[12,18,2,100] }, 
  { "vid":"bbb","san":[33,44] } 
]

How do I do that?

3
  • 2
    what do you think you need to do? What have you tried? Try to write this in a dummy language, just to understand how you can solve this problem. Then try to convert that to JavaScript and ask more specific questions about your problems. Commented Mar 24, 2013 at 16:28
  • I assume you've already parsed the JSON from a string into JavaScript data types, so I've removed the references to JSON to avoid confusion. Feel free to roll back if that's not the case. (btw it's actually not even an object but an array of objects.) Commented Mar 24, 2013 at 16:31
  • Juhana, yes, I have already done that using $.parseJSON(data). Commented Mar 24, 2013 at 16:33

4 Answers 4

2

There you go: http://jsfiddle.net/hezgb/

    var data = [ {"vid":"aaa", "san":12}, {"vid":"aaa", "san":18}, {"vid":"aaa", "san":2}, {"vid":"bbb", "san":33}, {"vid":"bbb", "san":44}, {"vid":"aaa", "san":100}];

function SerializeResults( inData ) {

    var map = {};
    $.each( inData, function( i, val ) {
         if(!map[val.vid]){
             var o = {};
             o.san = [];
             map[val.vid] = o;
         }
        else{
            var o = map[val.vid];
        }

        o.san.push(val.san);
    });
    var main = [];
    $.each( map, function( key, val ) {
        main.push({vid:key,san:val.san});  
    });

    return main;
}

console.log(SerializeResults(data));
Sign up to request clarification or add additional context in comments.

Comments

1

How does this work for you?

http://jsfiddle.net/T7bZd/4/

    $.each( inData, function( i, val ) 
{
   if(typeof out[val.vid]==="undefined")
       out[val.vid] = new Array();
   out[val.vid].push(val.san);
});

2 Comments

It is pretty, but doesn't give the results you asked for.
Yeah, I was hoping he could get away with something like this instead.
0

I am adding yet another answer as a form of punishment for you asking "how do I do that?" ;-)

var _ = require('underscore')
var json = '[ {"vid":"aaa", "san":12}, {"vid":"aaa", "san":18}, {"vid":"aaa", "san":2}, {"vid":"bbb", "san":33}, {"vid":"bbb", "san":44}, {"vid":"aaa", "san":100}]'

var grouped = _.groupBy(JSON.parse(json), function(z) {
  return z.vid
})

var result = _.pairs(grouped).map(function(item) {
  return { vid: item[0], san: _.pluck(item[1], 'san') }
})

2 Comments

Never heard of require('underscore') before, thanks for sharing!
FYI, this is actually server-side JS (node.js)
0

Try this.

function SerializeResults( inData ) {
    var a = {};

    $.each(inData, function( i, val ) {
        var id = val['vid'];

        if (a[id]) {
            a[id]['san'].push(val['san']);
        } else {
            a[id] = {vid:id, san:[val['san']]};
        }
    });

    var aa = [];

    $.each(a, function(i, val) {
        aa.push(val);
    });

    return aa;
}

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.