-1

I would like to merge objects here and would like to convert to JSON object. I would like to create an object array and add objects in to it. In this example i would like to create ConsData object array and add each dataset in to it.

I would like the data to be like
[
{

    "name":"aaa_aaaurf",
    "region":"F&R",
    "checkins":[[1,0],[2,0],[3,0],[4,3],[5,0],[6,0],[7,0],[8,3],[9,0],[10,0],[11,0],[12,0]],
    "teamsize":[[1,0],[2,0],[3,0],[4,3],[5,0],[6,0],[7,0],[8,1],[9,0],[10,0],[11,0],[12,0]],
    "Checkintimes":[[1,0],[2,0],[3,0],[4,184],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0],[12,0]]
},
{

    "name":"aaa_accessservices",
    "region":"F&R",
    "checkins":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,27],[12,12]],
    "teamsize":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,11],[12,11]],
    "Checkintimes":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,10],[12,12]]
}]

var ConsData = {};
var MergeData = {};


 for(var x=0;x<dataset.length;x++)
 {

       repository = dataset[x].repository;
        sbu = dataset[x].BusinessUnit
        checkinsarray.push([index, dataset[x].AvgCheckinCount]);
        teamsizearray.push([index, dataset[x].TeamSize]);
        checkintimesarray.push([index, dataset[x].MeanBuildTimeHrs]);

        ConsData["name"] = repository;
        ConsData["region"] = sbu;
        ConsData["checkins"] = checkinsarray;
        ConsData["teamsize"] = teamsizearray;
        ConsData["Checkintimes"] = checkintimesarray;
}

following is the data contained in dataset(fetched from csv file):

repository,month,year,MeanBuildTimeHrs,AvgCheckinCount,TeamSize,BusinessUnit
aaa_aaaurf,1,2013,0,0,0,Financial&Risk
aaa_aaaurf,2,2013,0,0,0,Financial&Risk
aaa_aaaurf,3,2013,0,0,0,Financial&Risk
aaa_aaaurf,4,2013,184,3,3,Financial&Risk
aaa_aaaurf,5,2013,0,0,0,Financial&Risk
aaa_aaaurf,6,2013,0,0,0,Financial&Risk
aaa_aaaurf,7,2013,0,0,0,Financial&Risk
aaa_aaaurf,8,2013,0,3,1,Financial&Risk
aaa_aaaurf,9,2013,0,0,0,Financial&Risk
aaa_aaaurf,10,2013,0,0,0,Financial&Risk
aaa_aaaurf,11,2013,0,0,0,Financial&Risk
aaa_aaaurf,12,2013,0,0,0,Financial&Risk
cCG_tzz,1,2013,5,3,100,Financial&Risk
cCG_tzz,2,2013,8,5,80,Financial&Risk
aCG_txz,1,2013,12,3,70,Financial&Risk
GCG_txz,1,2013,21,3,50,Financial&Risk
GCG_txz,2,2013,12,3,70,Financial&Risk
2
  • 1
    It's not completely clear what you actually want to merge. You're currently modifying ConsData multiple times. Do you want an array of objects like [{"name": repository, "region": sbu, ..}] as result? Also, your first paragraph sounds like a wish list for Christmas. Commented Jan 8, 2014 at 10:44
  • hi Zeta, I have added the format of the data to be merged. Sorry it is similar as the one specified. Commented Jan 8, 2014 at 10:57

2 Answers 2

1
var dataArray = [], i;

for(i = 0; i < dataset.length; i++)
{
    dataArray.push({
        "name":         dataset[x].repository,
        "region":       dataset[x].BusinessUnit,
        "checkins":     [index, dataset[x].AvgCheckinCount],
        "teamsize":     [index, dataset[x].TeamSize],
        "Checkintimes": [index, dataset[x].MeanBuildTimeHrs]
    });
}

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

You need to use a new object for every element in your array. Objects are stored by reference, and variables have function scope.

After having a look at your other questions, I recommend you to have a look at the JavaScript guide.

Sign up to request clarification or add additional context in comments.

Comments

0

Can you use lodash?

var names = {
  'characters': [
    { 'name': 'barney' },
    { 'name': 'fred' }
  ]
};

var ages = {
  'characters': [
    { 'age': 36 },
    { 'age': 40 }
  ]
};

_.merge(names, ages);
// → { 'characters': [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }] }

var food = {
  'fruits': ['apple'],
  'vegetables': ['beet']
};

var otherFood = {
  'fruits': ['banana'],
  'vegetables': ['carrot']
};

_.merge(food, otherFood, function(a, b) {
  return _.isArray(a) ? a.concat(b) : undefined;
});
// → { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot] }

3 Comments

Actually, I guess he doesn't want to merge two objects, but instead create a list/array of objects. Looking at his previous question, he misses to create a new object and overrides the same ConsData all the time.
@zeta but this answer needs to be here for anyone who is trying to figure out "how to merge objects in javascript".
Initially, i created an object and then using Jquery.extend tried to merge another object. However if i had to merge multiple objects i was having an issue. That's why i posted. However i got the issue resolved through zet. Instead of merging multiple objects, created alist of objects and added the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.