简体   繁体   English

如何格式化JSON对象数组(JavaScript)

[英]how to format JSON object array (Javascript)

I am struggling with a tough requirement where I don't know how to deal with 我在一个艰苦的要求中挣扎,我不知道该如何应对

I received one Array as below: 我收到了一个数组,如下所示:

[{
    "student": "21_A_1",
    "sport": "badminton"
}, {
    "student": "21_C_1",
    "sport": "badminton"
}, {
    "student": "21_A_2",
    "sport": "badminton"
}, {
    "student": "21_B_1",
    "sport": "football"
}, {
    "student": "21_A_2",
    "sport": "football"
}]

Requirement: convert it to an array with format : 要求:将其转换为以下格式的数组:

[{
    "student": "21_A_1",
    "sport": ["badminton"]
}, {
    "student": "21_C_1",
    "sport": ["badminton"]
}, {
    "student": "21_A_2",
    "sport": ["badminton","football"]
}, {
    "student": "21_B_1",
    "sport": ["football"]
}]

I found that lodash library has a feature "group by" returned quite closed result but not as expectation: 我发现lodash库具有“ group by”功能,返回的结果非常接近,但不符合预期:

_.groupBy(array, 'student');

Result: 结果:

{
    "21_A_1": [{
        "student": "21_A_1",
        "sport": "badminton"
    }],
    "21_C_1": [{
        "student": "21_C_1",
        "sport": "badminton"
    }],
    "21_A_2": [{
        "student": "21_A_2",
        "sport": "badminton"
    }, {
        "student": "21_A_2",
        "sport": "football"
    }],
    "21_B_1": [{
        "student": "21_B_1",
        "sport": "football"
    }]
}

Any suggestion is very appreciated. 任何建议都非常感谢。

We can use '.reduce' function of array and aggregate result as below 我们可以使用数组的“ .reduce”功能并汇总结果,如下所示

 var test = [{ "student": "21_A_1", "sport": "badminton" }, { "student": "21_C_1", "sport": "badminton" }, { "student": "21_A_2", "sport": "badminton" }, { "student": "21_B_1", "sport": "football" }, { "student": "21_A_2", "sport": "football" }] const result = test.reduce((res,x)=>{ res[x.student] = res[x.student] || { student:x.student,sport:[]} res[x.student].sport.push(x.sport) return res; },{}) console.log(result) 

You need to map the grouped items with their sport propery and build new objects with the grouping informations. 您需要将分组项目与其sport属性进行映射,并使用分组信息构建新对象。

 var array = [{ student: "21_A_1", sport: "badminton" }, { student: "21_C_1", sport: "badminton" }, { student: "21_A_2", sport: "badminton" }, { student: "21_B_1", sport: "football" }, { student: "21_A_2", sport: "football" }], grouped = _(array) .groupBy('student') .map((group, student) => ({ student, sport: _.map(group, 'sport') })) .value(); console.log(grouped); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script> 

Using Array.prototype.reduce() combined with Object.values() : 结合使用Array.prototype.reduce()Object.values()

 const data = [{"student": "21_A_1","sport": "badminton"}, {"student": "21_C_1","sport": "badminton"}, {"student": "21_A_2","sport": "badminton"}, {"student": "21_B_1","sport": "football"}, {"student": "21_A_2","sport": "football"}]; const result = Object.values( data.reduce((a, c) => ((a[c.student] = a[c.student] || {student: c.student, sport: []}).sport.push(c.sport), a), {}) ); console.log(result); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM