JSON 匹配 key/values 并获取每个键的总值
JSON to match key/values and get the total values for each key
我需要帮助显示我创建的 api 中每个 goalName 的总会话时间。我已经尝试映射和减少,但只能获得总会话时间。
我希望结果是这样的:
学习意大利语:6
意大利语中的 Parla con il mio amico : 1
"records": [
{
"sessionTime": 1,
"goalName": "Learn Italian",
"timeStapmed": "10:08:23",
"date": "11/08/2021",
"id": 1
},
{
"sessionTime": 5,
"goalName": "Learn Italian",
"id": "PFKFKZ",
"timeStapmed": "10:14:48",
"date": "11/08/2021"
},
{
"sessionTime": 1,
"goalName": "Parla con il mio amico in italiano",
"id": "aP_g1-",
"timeStapmed": "10:21:28",
"date": "11/08/2021"
}
]
先尝试filter
你想计算它们总和的元素。
const learnItalianTotal = records.filter(elm => elm.goalName === "Learn Italian")
.reduce((acc, cur) => acc.sessionTime + cur.sessionTime);
我认为您正在寻找 array#reduce
循环,
records.reduce(
(acc, { sessionTime, goalName }) =>
acc?.[goalName]
? { ...acc, [goalName]: acc[goalName] + sessionTime }
: { ...acc, [goalName]: sessionTime },
{}
);
我们可以遍历每个对象并将它们的值的总和保存在 acc
变量中。
完整代码:
let records = [
{
sessionTime: 1,
goalName: "Learn Italian",
timeStapmed: "10:08:23",
date: "11/08/2021",
id: 1,
},
{
sessionTime: 5,
goalName: "Learn Italian",
id: "PFKFKZ",
timeStapmed: "10:14:48",
date: "11/08/2021",
},
{
sessionTime: 1,
goalName: "Parla con il mio amico in italiano",
id: "aP_g1-",
timeStapmed: "10:21:28",
date: "11/08/2021",
},
];
let newRecords = records.reduce((acc, {sessionTime, goalName}) => acc?.[goalName] ? {...acc, [goalName]: acc[goalName] + sessionTime} : {...acc, [goalName]: sessionTime}, {})
console.log(newRecords)
试试这个
const records = [
{
sessionTime: 1,
goalName: 'Learn Italian',
timeStapmed: '10:08:23',
date: '11/08/2021',
id: 1
},
{
sessionTime: 5,
goalName: 'Learn Italian',
id: 'PFKFKZ',
timeStapmed: '10:14:48',
date: '11/08/2021'
},
{
sessionTime: 1,
goalName: 'Parla con il mio amico in italiano',
id: 'aP_g1-',
timeStapmed: '10:21:28',
date: '11/08/2021'
}
];
var result = records.reduce( ( accumulator, { sessionTime, goalName} ) => {
accumulator[goalName] = accumulator[ goalName ] ? (accumulator[goalName] + sessionTime): sessionTime;
return accumulator;
}, {} );
console.log(result);
输出
{
"Learn Italian": 6,
"Parla con il mio amico in italiano": 1
}
我需要帮助显示我创建的 api 中每个 goalName 的总会话时间。我已经尝试映射和减少,但只能获得总会话时间。
我希望结果是这样的:
学习意大利语:6
意大利语中的 Parla con il mio amico : 1
"records": [
{
"sessionTime": 1,
"goalName": "Learn Italian",
"timeStapmed": "10:08:23",
"date": "11/08/2021",
"id": 1
},
{
"sessionTime": 5,
"goalName": "Learn Italian",
"id": "PFKFKZ",
"timeStapmed": "10:14:48",
"date": "11/08/2021"
},
{
"sessionTime": 1,
"goalName": "Parla con il mio amico in italiano",
"id": "aP_g1-",
"timeStapmed": "10:21:28",
"date": "11/08/2021"
}
]
先尝试filter
你想计算它们总和的元素。
const learnItalianTotal = records.filter(elm => elm.goalName === "Learn Italian")
.reduce((acc, cur) => acc.sessionTime + cur.sessionTime);
我认为您正在寻找 array#reduce
循环,
records.reduce(
(acc, { sessionTime, goalName }) =>
acc?.[goalName]
? { ...acc, [goalName]: acc[goalName] + sessionTime }
: { ...acc, [goalName]: sessionTime },
{}
);
我们可以遍历每个对象并将它们的值的总和保存在 acc
变量中。
完整代码:
let records = [
{
sessionTime: 1,
goalName: "Learn Italian",
timeStapmed: "10:08:23",
date: "11/08/2021",
id: 1,
},
{
sessionTime: 5,
goalName: "Learn Italian",
id: "PFKFKZ",
timeStapmed: "10:14:48",
date: "11/08/2021",
},
{
sessionTime: 1,
goalName: "Parla con il mio amico in italiano",
id: "aP_g1-",
timeStapmed: "10:21:28",
date: "11/08/2021",
},
];
let newRecords = records.reduce((acc, {sessionTime, goalName}) => acc?.[goalName] ? {...acc, [goalName]: acc[goalName] + sessionTime} : {...acc, [goalName]: sessionTime}, {})
console.log(newRecords)
试试这个
const records = [
{
sessionTime: 1,
goalName: 'Learn Italian',
timeStapmed: '10:08:23',
date: '11/08/2021',
id: 1
},
{
sessionTime: 5,
goalName: 'Learn Italian',
id: 'PFKFKZ',
timeStapmed: '10:14:48',
date: '11/08/2021'
},
{
sessionTime: 1,
goalName: 'Parla con il mio amico in italiano',
id: 'aP_g1-',
timeStapmed: '10:21:28',
date: '11/08/2021'
}
];
var result = records.reduce( ( accumulator, { sessionTime, goalName} ) => {
accumulator[goalName] = accumulator[ goalName ] ? (accumulator[goalName] + sessionTime): sessionTime;
return accumulator;
}, {} );
console.log(result);
输出
{
"Learn Italian": 6,
"Parla con il mio amico in italiano": 1
}