我想在这里合并对象,并希望转换为JSON对象。我想创建一个对象数组并将对象添加到其中。在这个例子中,我想创建ConsData对象数组并将每个数据集添加到其中。
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;
}
以下是数据集中包含的数据(从csv文件中获取):
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
答案 0 :(得分: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));
您需要为数组中的每个元素使用 new 对象。对象由引用存储,变量具有 function 范围。
在查看了您的其他问题之后,我建议您查看JavaScript guide。
答案 1 :(得分:0)
您可以使用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] }