使用JavaScript / jQuery创建JSON数组

时间:2018-09-26 06:06:40

标签: javascript jquery arrays json

用户详细信息

 var UserDetails=[{"UserId":1,"Name":"Vineeth"},{"UserId":2,"Name":"Sreena"}]

通话详细信息

var CallSummary=[{"UserId":1,"UPerCalls":"22 %","UTotalCallTime":"60:24 minutes","UPerCallsTime":"0 %","UAvgCallTime":"0:22 minutes"},{"UserId":2,"UPerCalls":"8 %","UTotalCallTime":"50373:16 minutes","UPerCallsTime":"50 %","UAvgCallTime":"839:33 minutes"}]

预期结果

var Result=[{"UserId":1,"Name":"Vineeth","UPerCalls":"22 %","UTotalCallTime":"60:24 minutes","UPerCallsTime":"0 %","UAvgCallTime":"0:22 minutes"},{"UserId":2,"Name":"Sreena","UPerCalls":"8 %","UTotalCallTime":"50373:16 minutes","UPerCallsTime":"50 %","UAvgCallTime":"839:33 minutes"}]

如何获得此结果,我的代码是

var UserDetails=[{"UserId":1,"Name":"Vineeth"},{"UserId":2,"Name":"Sreena"}]

var CallSummary=[{"UserId":1,"UPerCalls":"22 %","UTotalCallTime":"60:24 minutes","UPerCallsTime":"0 %","UAvgCallTime":"0:22 minutes"},{"UserId":2,"UPerCalls":"8 %","UTotalCallTime":"50373:16 minutes","UPerCallsTime":"50 %","UAvgCallTime":"839:33 minutes"}]


var UserSummary = [];
$.each(UserDetails, function(key, index) {
    $.each(CallSummary, function(key, index1) {
        if (index.UserId == index1.UserId) {
            UserSummary.push({
                UserId: index.UserId
            });
            UserSummary.push({
                Name: index.Name
            });
            UserSummary.push({
                TotalPerCalls: index1.UPerCalls
            });
            UserSummary.push({
                TotalCallTime: index1.UTotalCallTime
            });
            UserSummary.push({
                TotalPerCallTime: index1.UPerCallsTime
            });
            UserSummary.push({
                TotalAvgCallTime: index1.UAvgCallTime
            });
        }
    })
})

console.log(UserSummary);

如何解决这个问题?

谢谢

6 个答案:

答案 0 :(得分:3)

您只需要一个迭代。通过执行.map,您可以遍历Users,并为每个用户在UserSummary数组中创建一个新对象。然后,在该对象内部,您{spread'对应于该用户的UserDetails对象的属性以及CallSummary的属性。

var UserDetails=[{"UserId":1,"Name":"Vineeth"},{"UserId":2,"Name":"Sreena"}];
var CallSummary=[{"UserId":1,"UPerCalls":"22 %","UTotalCallTime":"60:24 minutes","UPerCallsTime":"0 %","UAvgCallTime":"0:22 minutes"},{"UserId":2,"UPerCalls":"8 %","UTotalCallTime":"50373:16 minutes","UPerCallsTime":"50 %","UAvgCallTime":"839:33 minutes"}];

var UserSummary=UserDetails.map((user, index) => ({...user, ...CallSummary[index]}));

console.log(UserSummary);

答案 1 :(得分:1)

只需使用gradle.properties即可返回Object.assign({}, index, index1)index的组合结果。

index1
var UserDetails=[{"UserId":1,"Name":"Vineeth"},{"UserId":2,"Name":"Sreena"}]

var CallSummary=[{"UserId":1,"UPerCalls":"22 %","UTotalCallTime":"60:24 minutes","UPerCallsTime":"0 %","UAvgCallTime":"0:22 minutes"},{"UserId":2,"UPerCalls":"8 %","UTotalCallTime":"50373:16 minutes","UPerCallsTime":"50 %","UAvgCallTime":"839:33 minutes"}]

var UserSummary = [];
$.each(UserDetails, function(key, index) {
    $.each(CallSummary, function(key, index1) {
        if (index.UserId == index1.UserId) {
            UserSummary.push(Object.assign({}, index, index1));
        }
    })
})
console.log(UserSummary);
 

答案 2 :(得分:1)

您可以使用.concat().reduce()Object.assign()之类的本机函数来获取此信息:

var UserDetails = [{"UserId":1,"Name":"Vineeth"},{"UserId":2,"Name":"Sreena"}];
var CallSummary = [{"UserId":1,"UPerCalls":"22 %","UTotalCallTime":"60:24 minutes","UPerCallsTime":"0 %","UAvgCallTime":"0:22 minutes"},{"UserId":2,"UPerCalls":"8 %","UTotalCallTime":"50373:16 minutes","UPerCallsTime":"50 %","UAvgCallTime":"839:33 minutes"}];

var result = Object.values(
               UserDetails
                .concat(CallSummary)
                .reduce((r, c) => (r[c["UserId"]] = Object.assign({}, r[c["UserId"]], c), r), [])
             );
                        
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

参考:

答案 3 :(得分:1)

请尝试以下代码:) 您需要将这些值分配给一个临时对象,然后将该对象推入数组。

var UserDetails=[{"UserId":1,"Name":"Vineeth"},{"UserId":2,"Name":"Sreena"}];
var CallSummary=[{"UserId":1,"UPerCalls":"22 %","UTotalCallTime":"60:24 minutes","UPerCallsTime":"0 %","UAvgCallTime":"0:22 minutes"},{"UserId":2,"UPerCalls":"8 %","UTotalCallTime":"50373:16 minutes","UPerCallsTime":"50 %","UAvgCallTime":"839:33 minutes"}];

UserSummary=[];
        $.each(UserDetails, function(key, index) {
            $.each(CallSummary, function(key, index1) {
                if(index.UserId==index1.UserId){
                    var tempObject = {};
                    tempObject['UserId'] = index.UserId;
                    tempObject['Name'] = index.Name;
                    tempObject['TotalCalls'] = index1.TotalCalls;
                    tempObject['TotalPerCalls'] = index1.UPerCalls;
                    tempObject['TotalCallTime'] = index1.UTotalCallTime;
                    tempObject['TotalPerCallTime'] = index1.UPerCallsTime;
                    tempObject['TotalAvgCallTime'] = index1.UAvgCallTime;
                    UserSummary.push(tempObject);
                }
            })
       })

 console.log(UserSummary);

答案 4 :(得分:0)

您需要执行以下步骤:

  1. 环绕UserDetails数组以获取该数组中的每个对象
  2. 然后在CallSummary数组中找到具有匹配的UserId值的对象
  3. 如果UserIdObject.assign()相同,则合并对象
  4. 将新对象推入数组UserSummary

var UserDetails=[{"UserId":1,"Name":"Vineeth"},{"UserId":2,"Name":"Sreena"}];

var CallSummary=[{"UserId":1,"UPerCalls":"22 %","UTotalCallTime":"60:24 minutes","UPerCallsTime":"0 %","UAvgCallTime":"0:22 minutes"},{"UserId":2,"UPerCalls":"8 %","UTotalCallTime":"50373:16 minutes","UPerCallsTime":"50 %","UAvgCallTime":"839:33 minutes"}];

var UserSummary = [];
UserDetails.forEach((userDetail) => {
  var callSummary = CallSummary.find(({UserId})=> UserId === userDetail.UserId);
  var newObj = Object.assign(userDetail, callSummary);
  UserSummary.push(newObj);
});
console.log(UserSummary);

答案 5 :(得分:0)

这很简单:请按如下所示编写逻辑

var UserDetails=[{"UserId":1,"Name":"Vineeth"},{"UserId":2,"Name":"Sreena"}];
var CallSummary=[{"UserId":1,"UPerCalls":"22 %","UTotalCallTime":"60:24 minutes","UPerCallsTime":"0 %","UAvgCallTime":"0:22 minutes"},{"UserId":2,"UPerCalls":"8 %","UTotalCallTime":"50373:16 minutes","UPerCallsTime":"50 %","UAvgCallTime":"839:33 minutes"}]
var array3 = CallSummary.filter(function(obj) { return UserDetails.indexOf(obj.UserId) == -1; });

console.log(array3);

请参见here