Bài toán kinh điển sử dụng three dots lấy tất cả các key của object, key same, plus (ok)
Ví dụ 1: Lấy tất cả các key của object (ok)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var test = [{
key1: 1,
key2: 2
}, {
key2: 2,
key3: 3,
key6: 5
}, {
key3: 3,
key4: 4
}, {
key4: 4,
key5: 5
}];
// console.log(Object.getOwnPropertyNames(testkey));
var result = test.reduce(function(sum,now){
return {...sum,...now};
},{});
console.log(result);
</script>
</body>
</html>
Ví dụ 2: Mở rộng ví dụ tính tổng các key trùng nhau (P1)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
var test = [{
key1: 1,
key2: 2
}, {
key2: 2,
key3: 3,
key6: 5
}, {
key3: 3,
key4: 4
}, {
key1: 100,
key4: 4,
key5: 5
}];
var result = test.reduce(function(sum,now){
return {...sum,...now};
},{});
var arrs = Object.keys(result);
var tong1,tong2,tong3,tong4,tong5,tong6;
tong1 = tong2 = tong3 = tong4 = tong5 = tong6 = 0;
var total = test.reduce(function(sum,now){
if(now.hasOwnProperty(arrs[0])) {
tong1 += now[arrs[0]];
}
if(now.hasOwnProperty(arrs[1])) {
tong2 += now[arrs[1]];
}
if(now.hasOwnProperty(arrs[2])) {
tong3 += now[arrs[2]];
}
if(now.hasOwnProperty(arrs[3])) {
tong4 += now[arrs[3]];
}
if(now.hasOwnProperty(arrs[4])) {
tong5 += now[arrs[4]];
}
if(now.hasOwnProperty(arrs[5])) {
tong6 += now[arrs[5]];
}
return {tong1: tong1,tong2: tong2,tong3: tong3,tong4: tong4,tong5: tong5,tong6: tong6}
},0);
console.log(total);
</script>
</body>
</html>
Ví dụ 2: Mở rộng ví dụ tính tổng các key trùng nhau (P2)
var obj = [
{ 'name': 'P1', 'value': 150 },
{ 'name': 'P1', 'value': 150 },
{ 'name': 'P2', 'value': 200 },
{ 'name': 'P3', 'value': 450 },
{ 'name': 'P1', 'value': 300 }
];
var holder = {};
obj.forEach(function(d) {
if (holder.hasOwnProperty(d.name)) {
holder[d.name] = holder[d.name] + d.value;
} else {
holder[d.name] = d.value;
}
});
var obj2 = [];
for (var prop in holder) {
obj2.push({ name: prop, value: holder[prop] });
}
console.log(obj2);
Ví dụ 2: Tổng 2 khóa đối tượng giống nhau
https://stackoverflow.com/questions/42488048/how-can-i-sum-properties-from-two-objects
var obj1 = {
a: 12,
b: 8,
c: 17
};
var obj2 = {
a: 12,
b: 8,
c: 17
};
var obj3 = {
a: 12,
b: 8,
c: 17
};
function sumObjectsByKey(...objs) {
return objs.reduce((a, b) => {
for (let k in b) {
if (b.hasOwnProperty(k))
a[k] = (a[k] || 0) + b[k];
}
return a;
}, {});
}
console.log(sumObjectsByKey(obj1, obj2, obj3));
Bài toán 2:
var data = [
{
"id": 1,
"name": "A1",
"day": "mon",
"candidate": 5
},
{
"id": 2,
"name": "A2",
"day": "mon",
"candidate": 2
},
{
"id": 3,
"name": "A3",
"day": "mon",
"candidate": 9
},
{
"id": 4,
"name": "B1",
"day": "tue",
"candidate": 5
}
];
// var result = {
// monday: [
// { "id": 1, "name": "A1", "day": "mon", "candidate": 5 },
// { "id": 2, "name": "A2", "day": "mon", "candidate": 2 },
// { "id": 3, "name": "A3", "day": "mon", "candidate": 9 }
// ],
// tuesday: [
// { "id": 4, "name": "B1", "day": "tue", "candidate": 5 }
// ]
// }
// ===
const days = [
{
"id": 1,
"name": "A",
"day": "mon",
"candidate": 5
},
{
"id": 2,
"name": "A",
"day": "mon",
"candidate": 4
},
{
"id": 3,
"name": "A",
"day": "mon",
"candidate": 3
},
{
"id": 4,
"name": "A",
"day": "tue",
"candidate": 2
}
]
function getTH(dayString) {
switch(dayString) {
case "mon": return "Monday";
case "tue": return "Tuesday";
case "wed": return "Wednesday";
case "thu": return "Thursday";
case "fri": return "Friday";
case "sat": return "Saturday";
case "sun": return "Sunday";
}
}
let newDaysFormatObject = {
Monday: [],
Tuesday: [],
Wednesday: [],
Thursday: [],
Friday: [],
Saturday: [],
Sunday: []
};
days.forEach(d => {
const dayTH = getTH(d.day);
const {day, name, candidate} = d;
newDaysFormatObject[dayTH] = [...newDaysFormatObject[dayTH], {day, name, candidate}]
});
// console.log(newDaysFormatObject);
// ===
function groupByKey(array,key) {
return array.reduce((hask,obj)=>{
if(obj[key] === undefined) return hask;
return Object.assign(hask, {
[obj[key]] : (hask[obj[key]] || []).concat(obj)
},{});
});
}
const cars = [
{
"id": 1,
"name": "A",
"day": "mon",
"candidate": 5
},
{
"id": 2,
"name": "A",
"day": "mon",
"candidate": 4
},
{
"id": 3,
"name": "A",
"day": "mon",
"candidate": 3
},
{
"id": 4,
"name": "A",
"day": "tue",
"candidate": 2
}
];
console.log(groupByKey(cars,"day"));
Bài toán
"personas": [
{
"category":"Persona1",
"month":"6",
"week":"24",
"day":"18",
"dayaverage":0.5,
"weekaverage":1.33333333333333,
"monthaverage":1.53571428571429
},
{
"category":"Persona2",
"month":"6",
"week":"24",
"day":"18",
"dayaverage":0.146477031224456,
"weekaverage":0.194758246723904,
"monthaverage":0.601273296708939
},
{
"category":"Persona3",
"month":"6",
"week":"24",
"day":"18",
"dayaverage":1.25559947299078,
"weekaverage":1.43618513323983,
"monthaverage":0.998426393184655
},
{
"category":"Persona4",
"month":"6",
"week":"24",
"day":"18",
"dayaverage":0.799332962757087,
"weekaverage":0.923262727610554,
"monthaverage":0.769477297163179
},
{
"category":"Persona5",
"month":"6",
"week":"24",
"day":"18",
"dayaverage":0.669041769041769,
"weekaverage":0.67394482002558,
"monthaverage":0.670944920469891
},
{
"category":"Persona6",
"month":"6",
"week":"24",
"day":"18",
"dayaverage":0.656381486676017,
"weekaverage":0.722973507315144,
"monthaverage":0.69689774371321
},
{
"category":"Persona7",
"month":"6",
"week":"24",
"day":"18",
"dayaverage":0.540495407737267,
"weekaverage":0.576413277444205,
"monthaverage":0.693495281755596
}
]
Out format
[
{
name: 'dayaverage',
data: [0.5, 0.146477031224456, 1.25559947299078, 0.799332962757087, 0.669041769041769, 0.656381486676017, 0.540495407737267]
},
{
name: 'weekaverage',
data: [1.33333333333333, 0.194758246723904, 1.43618513323983, 0.923262727610554, 0.67394482002558, 0.722973507315144, 0.576413277444205]
}, {
name: 'monthaverage',
data: [1.53571428571429, 0.601273296708939, 0.998426393184655, 0.769477297163179, 0.670944920469891, 0.69689774371321, 0.693495281755596]
}
].
https://stackoverflow.com/questions/42814907/group-values-with-the-same-key-in-an-array-of-objects
var personas = [{
"category": "Persona1",
"month": "6",
"week": "24",
"day": "18",
"dayaverage": 0.5,
"weekaverage": 1.33333333333333,
"monthaverage": 1.53571428571429
},
{
"category": "Persona2",
"month": "6",
"week": "24",
"day": "18",
"dayaverage": 0.146477031224456,
"weekaverage": 0.194758246723904,
"monthaverage": 0.601273296708939
},
{
"category": "Persona3",
"month": "6",
"week": "24",
"day": "18",
"dayaverage": 1.25559947299078,
"weekaverage": 1.43618513323983,
"monthaverage": 0.998426393184655
},
{
"category": "Persona4",
"month": "6",
"week": "24",
"day": "18",
"dayaverage": 0.799332962757087,
"weekaverage": 0.923262727610554,
"monthaverage": 0.769477297163179
},
{
"category": "Persona5",
"month": "6",
"week": "24",
"day": "18",
"dayaverage": 0.669041769041769,
"weekaverage": 0.67394482002558,
"monthaverage": 0.670944920469891
},
{
"category": "Persona6",
"month": "6",
"week": "24",
"day": "18",
"dayaverage": 0.656381486676017,
"weekaverage": 0.722973507315144,
"monthaverage": 0.69689774371321
},
{
"category": "Persona7",
"month": "6",
"week": "24",
"day": "18",
"dayaverage": 0.540495407737267,
"weekaverage": 0.576413277444205,
"monthaverage": 0.693495281755596
}
];
var criteria = ['dayaverage', 'weekaverage', 'monthaverage'];
function getMerged(objArr, criteria) {
var dataMap = objArr.reduce(function (result, current) {
criteria.forEach(function (elem) {
if (result[elem] != undefined) {
result[elem].push(current[elem]);
}
else {
result[elem] = [current[elem]];
}
});
return result;
}, {});
return criteria.map(function (elem) {
return {
name: elem,
data: dataMap[elem]
};
});
}
console.log(getMerged(personas, criteria));
Ví dụ 3:
var data = [
{ name: "fieldA", value: 20,value1: 31 },
{ name: "building", value: 21,value1: 32 },
{ name: "fieldA", value: 22,value1: 33 },
{ name: "building", value: 23,value1: 34 }
];
var result = Object.values(data.reduce((acc, { name, value,value1 }) => {
acc[name] = acc[name] || { name, value: 0,value1:0 };
acc[name].value += value;
acc[name].value1 += value1;
return acc;
}, {}));
console.log(result);
Một bài toán kinh điển xử lý bên kintone
var arrs = [{
"app": 187,
"id": "1",
"record": {
"定時内時間計": {
"value": 8
},
"残業時間計": {
"value": 1.25
},
"作業時間合計": {
"value": 9.25
},
"現金区分": {
"value": "現金"
},
"数値_15": 8,
"工程B": 22,
"工程C": 0
}
}, {
"app": 187,
"id": "2",
"record": {
"定時内時間計": {
"value": 2
},
"残業時間計": {
"value": 0
},
"作業時間合計": {
"value": 2
},
"現金区分": {
"value": "現金"
},
"数値_15": 0,
"工程B": 0,
"工程C": 2
}
}, {
"app": 187,
"id": "8",
"record": {
"定時内時間計": {
"value": 2
},
"残業時間計": {
"value": 0
},
"作業時間合計": {
"value": 2
},
"現金区分": {
"value": "現金"
},
"数値_15": 2,
"工程B": 0,
"工程C": 0
}
}, {
"app": 187,
"id": "6",
"record": {
"定時内時間計": {
"value": 2
},
"残業時間計": {
"value": 0
},
"作業時間合計": {
"value": 2
},
"現金区分": {
"value": "現金"
},
"数値_15": 2,
"工程B": 2,
"工程C": 3
}
}, {
"app": 187,
"id": "6",
"record": {
"定時内時間計": {
"value": 2
},
"残業時間計": {
"value": 0
},
"作業時間合計": {
"value": 2
},
"現金区分": {
"value": "現金"
},
"数値_15": 2,
"工程B": 2,
"工程C": 3
}
}, {
"app": 187,
"id": "6",
"record": {
"定時内時間計": {
"value": 3
},
"残業時間計": {
"value": 0
},
"作業時間合計": {
"value": 2
},
"現金区分": {
"value": "現金"
},
"数値_15": 2,
"工程B": 2,
"工程C": 3
}
}];
Bài toán:
Giữ nguyên định dạng của mảng, những id nào trùng nhau thì cộng lại giá số (ok)
const results = arrs.reduce((acc, val, record) => {
const findName = acc.find(_ => _.id === val.id);
if (findName) {
var one = (findName.record.定時内時間計.value) ? (findName.record.定時内時間計.value) : 0;
var _one = (val.record.定時内時間計.value) ? (val.record.定時内時間計.value) : 0;
var two = (findName.record.残業時間計.value) ? (findName.record.残業時間計.value) : 0;
var _two = (val.record.残業時間計.value) ? (val.record.残業時間計.value) : 0;
var three = one + _one + two + _two;
findName.app = val.app;
findName.record.定時内時間計 = {value: one + _one};
findName.record.残業時間計 = {value: two + _two};
findName.record.作業時間合計 = {value: three};
findName.record.数値_15 = findName.record.数値_15 + val.record.数値_15;
findName.record.工程B = findName.record.工程B + val.record.工程B;
findName.record.工程C = findName.record.工程C + val.record.工程C;
} else {
acc.push(val);
}
return acc;
}, []);
console.log(results);
Kết quả:
[
{
"app": 187,
"id": "1",
"record": {
"定時内時間計": {
"value": 8
},
"残業時間計": {
"value": 1.25
},
"作業時間合計": {
"value": 9.25
},
"現金区分": {
"value": "現金"
},
"数値_15": 8,
"工程B": 22,
"工程C": 0
}
},
{
"app": 187,
"id": "2",
"record": {
"定時内時間計": {
"value": 2
},
"残業時間計": {
"value": 0
},
"作業時間合計": {
"value": 2
},
"現金区分": {
"value": "現金"
},
"数値_15": 0,
"工程B": 0,
"工程C": 2
}
},
{
"app": 187,
"id": "8",
"record": {
"定時内時間計": {
"value": 2
},
"残業時間計": {
"value": 0
},
"作業時間合計": {
"value": 2
},
"現金区分": {
"value": "現金"
},
"数値_15": 2,
"工程B": 0,
"工程C": 0
}
},
{
"app": 187,
"id": "6",
"record": {
"定時内時間計": {
"value": 7
},
"残業時間計": {
"value": 0
},
"作業時間合計": {
"value": 7
},
"現金区分": {
"value": "現金"
},
"数値_15": 6,
"工程B": 6,
"工程C": 9
}
}
]
Last updated