Skip to main content
=== is better than == since it considers the types
Source Link
Juneyoung Oh
  • 321
  • 1
  • 4
  • 12
function groupBy(coll, f) {
  return coll.reduce(function(acc, x) {
    var k = f(x);
    acc[k] = (acc[k] || []).concat(x);
    return acc;
  }, {});
}

var test = {
  items: [{
    setId: 'setA', prodId: "A", price: '5', deliveryCost : '1', name: "Set_Prod01"
  }, {
    setId: 'setA', prodId: "B", price :'5', deliveryCost : '2', name: "Set_Prod01"
  }]
};

console.log(JSON.stringify(test));

var result = groupBy(test.items, function(x){return x.setId});
//first grouping
console.log(JSON.stringify(result));

//begin spagetti code... T^T
var secondResult = {};
for(var key in result){
    var secondObjForSet = {};
    var setGroup = result[key]; 
    for(var i = 0; i < setGroup.length; i++) {
        var singleSetItem = setGroup[i];
        for(var innerKey in singleSetItem){
            if(innerKey == 'deliveryCost'){
                secondObjForSet[innerKey] = (typeof secondObjForSet[innerKey] ===== 'undefined') ? 0 + singleSetItem[innerKey] : (secondObjForSet[innerKey] * 1) + (singleSetItem[innerKey] * 1);
            }else if(innerKey == 'prodId'){
                secondObjForSet[innerKey] = 
                    (typeof secondObjForSet[innerKey] ===== 'undefined' || secondObjForSet[innerKey] == '') ? 
                        singleSetItem[innerKey] : secondObjForSet[innerKey] + ',' + singleSetItem[innerKey];
            }
            else{
                secondObjForSet[innerKey] = singleSetItem[innerKey];
            }
        }
    }
    secondResult[key] = secondObjForSet;
}

console.log(JSON.stringify(secondResult));
function groupBy(coll, f) {
  return coll.reduce(function(acc, x) {
    var k = f(x);
    acc[k] = (acc[k] || []).concat(x);
    return acc;
  }, {});
}

var test = {
  items: [{
    setId: 'setA', prodId: "A", price: '5', deliveryCost : '1', name: "Set_Prod01"
  }, {
    setId: 'setA', prodId: "B", price :'5', deliveryCost : '2', name: "Set_Prod01"
  }]
};

console.log(JSON.stringify(test));

var result = groupBy(test.items, function(x){return x.setId});
//first grouping
console.log(JSON.stringify(result));

//begin spagetti code... T^T
var secondResult = {};
for(var key in result){
    var secondObjForSet = {};
    var setGroup = result[key]; 
    for(var i = 0; i < setGroup.length; i++) {
        var singleSetItem = setGroup[i];
        for(var innerKey in singleSetItem){
            if(innerKey == 'deliveryCost'){
                secondObjForSet[innerKey] = (typeof secondObjForSet[innerKey] == 'undefined') ? 0 + singleSetItem[innerKey] : (secondObjForSet[innerKey] * 1) + (singleSetItem[innerKey] * 1);
            }else if(innerKey == 'prodId'){
                secondObjForSet[innerKey] = 
                    (typeof secondObjForSet[innerKey] == 'undefined' || secondObjForSet[innerKey] == '') ? 
                        singleSetItem[innerKey] : secondObjForSet[innerKey] + ',' + singleSetItem[innerKey];
            }
            else{
                secondObjForSet[innerKey] = singleSetItem[innerKey];
            }
        }
    }
    secondResult[key] = secondObjForSet;
}

console.log(JSON.stringify(secondResult));
function groupBy(coll, f) {
  return coll.reduce(function(acc, x) {
    var k = f(x);
    acc[k] = (acc[k] || []).concat(x);
    return acc;
  }, {});
}

var test = {
  items: [{
    setId: 'setA', prodId: "A", price: '5', deliveryCost : '1', name: "Set_Prod01"
  }, {
    setId: 'setA', prodId: "B", price :'5', deliveryCost : '2', name: "Set_Prod01"
  }]
};

console.log(JSON.stringify(test));

var result = groupBy(test.items, function(x){return x.setId});
//first grouping
console.log(JSON.stringify(result));

//begin spagetti code... T^T
var secondResult = {};
for(var key in result){
    var secondObjForSet = {};
    var setGroup = result[key]; 
    for(var i = 0; i < setGroup.length; i++) {
        var singleSetItem = setGroup[i];
        for(var innerKey in singleSetItem){
            if(innerKey == 'deliveryCost'){
                secondObjForSet[innerKey] = (typeof secondObjForSet[innerKey] === 'undefined') ? 0 + singleSetItem[innerKey] : (secondObjForSet[innerKey] * 1) + (singleSetItem[innerKey] * 1);
            }else if(innerKey == 'prodId'){
                secondObjForSet[innerKey] = 
                    (typeof secondObjForSet[innerKey] === 'undefined' || secondObjForSet[innerKey] == '') ? 
                        singleSetItem[innerKey] : secondObjForSet[innerKey] + ',' + singleSetItem[innerKey];
            }
            else{
                secondObjForSet[innerKey] = singleSetItem[innerKey];
            }
        }
    }
    secondResult[key] = secondObjForSet;
}

console.log(JSON.stringify(secondResult));
deleted 11 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

merging json object Merging JSON objects with common values

Hi I am trying to merging jsonmerge JSON objects which hashave common valuevalues.

This is the original JSON:

Since it has same setId and name, I want to make it like belowthis:

toTo do this, I made a javascriptthis JavaScript code like below, but it looks terrible and more the elements hashave attributes morewith if else willthan needed.

This is not flexible at all. So, so I need some help to make it more efficiently. Thanksefficient.

merging json object

Hi I am trying to merging json objects which has common value.

This is original JSON

Since it has same setId and name, I want to make like below

to do this, I made a javascript code like below, but it looks terrible and more the elements has attributes more if else will needed.

not flexible at all. So I need some help to make it more efficiently. Thanks.

Merging JSON objects with common values

I am trying to merge JSON objects which have common values.

This is the original JSON:

Since it has same setId and name, I want to make it like this:

To do this, I made this JavaScript code, but it looks terrible and more elements have attributes with if else than needed.

This is not flexible at all, so I need some help to make it more efficient.

Source Link
Juneyoung Oh
  • 321
  • 1
  • 4
  • 12

merging json object

Hi I am trying to merging json objects which has common value.

This is original JSON

{
    "setA":[
        {
            "setId":"setA"
            ,"prodId":"A"
            ,"price":"5"
            ,"delivertCost":"1"
            ,"name":"Set_Prod01"
        }

        ,{
            "setId":"setA"
            ,"prodId":"B"
            ,"price":"5"
            ,"delivertCost":"1"
            ,"name":"Set_Prod01"
        }
    ]
}

Since it has same setId and name, I want to make like below

{
    "setA": {
        "setId":"setA"
        ,"prodId":"A,B"
        ,"price":"5"
        ,"deliveryCost":3
        ,"name":"Set_Prod01"
    }
}

to do this, I made a javascript code like below, but it looks terrible and more the elements has attributes more if else will needed.

function groupBy(coll, f) {
  return coll.reduce(function(acc, x) {
    var k = f(x);
    acc[k] = (acc[k] || []).concat(x);
    return acc;
  }, {});
}

var test = {
  items: [{
    setId: 'setA', prodId: "A", price: '5', deliveryCost : '1', name: "Set_Prod01"
  }, {
    setId: 'setA', prodId: "B", price :'5', deliveryCost : '2', name: "Set_Prod01"
  }]
};

console.log(JSON.stringify(test));

var result = groupBy(test.items, function(x){return x.setId});
//first grouping
console.log(JSON.stringify(result));

//begin spagetti code... T^T
var secondResult = {};
for(var key in result){
    var secondObjForSet = {};
    var setGroup = result[key]; 
    for(var i = 0; i < setGroup.length; i++) {
        var singleSetItem = setGroup[i];
        for(var innerKey in singleSetItem){
            if(innerKey == 'deliveryCost'){
                secondObjForSet[innerKey] = (typeof secondObjForSet[innerKey] == 'undefined') ? 0 + singleSetItem[innerKey] : (secondObjForSet[innerKey] * 1) + (singleSetItem[innerKey] * 1);
            }else if(innerKey == 'prodId'){
                secondObjForSet[innerKey] = 
                    (typeof secondObjForSet[innerKey] == 'undefined' || secondObjForSet[innerKey] == '') ? 
                        singleSetItem[innerKey] : secondObjForSet[innerKey] + ',' + singleSetItem[innerKey];
            }
            else{
                secondObjForSet[innerKey] = singleSetItem[innerKey];
            }
        }
    }
    secondResult[key] = secondObjForSet;
}

console.log(JSON.stringify(secondResult));

not flexible at all. So I need some help to make it more efficiently. Thanks.