我在这个模式中有一个数组对象。
[
{student_code: "AAA-001-ZYrq", questions: "Height (in cm)", answer: "4543435", time: "00:00:07:00"},
{student_code: "AAA-001-6Yj8", questions: "Height (in cm)", answer: "4334", time: "00:00:02:32"},
{student_code: "AAA-001-ZYrq", questions: "Weight (in kg)", answer: "2345", time: "00:00:01:999"},
{student_code: "AAA-001-6Yj8", questions: "Weight (in kg)", answer: "34", time: "00:00:04:969"}
]
我想通过student_code减少对它的重新格式化,并将值存储在数组中。
{
student_code: "AAA-001-ZYrq",
questions: [
{
question: "Height (in cm)",
answer: "4543435",
time: "00:00:07:00"
},
{
question: "Weight (in kg)",
answer: "2345",
time: "00:00:01:999"
}
]
}
希望有人能帮助我。谢谢。
您可以使用Array.reduce
函数来实现该结构。
const inputValue = [
{student_code: "AAA-001-ZYrq", questions: "Height (in cm)", answer: "4543435", time: "00:00:07:00"},
{student_code: "AAA-001-6Yj8", questions: "Height (in cm)", answer: "4334", time: "00:00:02:32"},
{student_code: "AAA-001-ZYrq", questions: "Weight (in kg)", answer: "2345", time: "00:00:01:999"},
{student_code: "AAA-001-6Yj8", questions: "Weight (in kg)", answer: "34", time: "00:00:04:969"}
];
const groupBy = (array, key) => {
return array.reduce((result, currentValue) => {
(result[currentValue[key]] = result[currentValue[key]] || []).push(
currentValue
);
return result;
}, {});
};
const groupedValue = groupBy(inputValue, 'student_code');
const result = Object.entries(groupedValue).map((item) => ({
student_code: item[0],
questions: item[1]
}));
console.log(result); // Will show what you want.
首先创建一个student_code
的Map
到问题数组,然后将该Map
转换为最终数组结果
// Polyfill
Map.prototype.computeIfAbsent = function(key, mappingFunction) {
if (this.has(key)) {
return this.get(key)
}
return this.set(key, mappingFunction(key)).get(key)
}
// The array from your question (minified)
const arr = [{"student_code":"AAA-001-ZYrq","questions":"Height (in cm)","answer":"4543435","time":"00:00:07:00"},{"student_code":"AAA-001-6Yj8","questions":"Height (in cm)","answer":"4334","time":"00:00:02:32"},{"student_code":"AAA-001-ZYrq","questions":"Weight (in kg)","answer":"2345","time":"00:00:01:999"},{"student_code":"AAA-001-6Yj8","questions":"Weight (in kg)","answer":"34","time":"00:00:04:969"}]
// Create a map of `student_code` to questions array
const studentMap = arr.reduce((map, { student_code, questions: question, ...other }) => {
const questions = map.computeIfAbsent(student_code, () => [])
questions.push({ question, ...other})
return map
}, new Map())
// Transform to the final result
const result = Array.from(studentMap, ([ student_code, questions ]) => ({
student_code,
questions
}))
console.log(result)
.as-console-wrapper { max-height:100% !important;}
我会先创建一个映射,然后使用映射中的键创建一个数组。
// Assume the data variable is your existing array of objects
const data = [
{student_code: "AAA-001-ZYrq", questions: "Height (in cm)", answer: "4543435", time: "00:00:07:00"},
{student_code: "AAA-001-6Yj8", questions: "Height (in cm)", answer: "4334", time: "00:00:02:32"},
{student_code: "AAA-001-ZYrq", questions: "Weight (in kg)", answer: "2345", time: "00:00:01:999"},
{student_code: "AAA-001-6Yj8", questions: "Weight (in kg)", answer: "34", time: "00:00:04:969"}
];
const codeMap = {};
for(let i = 0; i < data.length; i++){
const entry = data[i];
// Initialize an empty array for this student code if not already
if(! codeMap[entry.student_code] ){
codeMap[entry.student_code] = [];
}
codeMap[entry.student_code].push({
question: entry.questions,
answer: entry.answer,
time: entry.time
})
}
// Now that you've created a map, you can turn it into an array
const reducedArray = [];
const studentCodes = Object.keys(codeMap);
for(let x = 0 ; x < studentCodes.length; x++){
const code = studentCodes[x];
reducedArray.push({
student_code: code,
questions: codeMap[code]
});
}
// Now the reducedArray variable should have what you want
console.log(reducedArray)
以下是我的操作方法。只需我的两美分。
const data = [
{student_code: "AAA-001-ZYrq", questions: "Height (in cm)", answer: "4543435", time: "00:00:07:00"},
{student_code: "AAA-001-6Yj8", questions: "Height (in cm)", answer: "4334", time: "00:00:02:32"},
{student_code: "AAA-001-ZYrq", questions: "Weight (in kg)", answer: "2345", time: "00:00:01:999"},
{student_code: "AAA-001-6Yj8", questions: "Weight (in kg)", answer: "34", time: "00:00:04:969"}
];
//build a datum(s) by code lookup
const datumsByCode = {};
for (let datum of data) {
const code = datum.student_code;
datumsByCode[code] = datumsByCode[code] || [];
datumsByCode[code].push(datum);
}
const newData = [];
for (let code in datumsByCode) {
const datums = datumsByCode[code];
const newDatum = {
student_code: code,
questions: datums.map(datum => ({
question: datum.questions,
answer: datum.answer,
time: datum.time
}))
};
newData.push(newDatum);
}
console.log(newData);
arr是您提到的数组对象,
const result = arr.reduce((a, c) => {
if (a.some(el => el.student_code === c.student_code)) {
let index = a.map(el => el.student_code).indexOf(c.student_code);
a[index].questions.push({
question: c.questions,
answer: c.asnwer,
time: c.time
});
} else {
a.push({
student_code: c.student_code,
questions: [{ question: c.questions, answer: c.asnwer, time: c.time }]
});
}
return a;
}, []);
使用Array reduce方法
let arr = [{
student_code: "AAA-001-ZYrq",
questions: "Height (in cm)",
answer: "4543435",
time: "00:00:07:00"
},
{
student_code: "AAA-001-6Yj8",
questions: "Height (in cm)",
answer: "4334",
time: "00:00:02:32"
},
{
student_code: "AAA-001-ZYrq",
questions: "Weight (in kg)",
answer: "2345",
time: "00:00:01:999"
},
{
student_code: "AAA-001-6Yj8",
questions: "Weight (in kg)",
answer: "34",
time: "00:00:04:969"
}
]
let result = arr.reduce((a, c) => {
let std = a.find(e => e.student_code == c.student_code);
if (!std) {
std = {
student_code: c.student_code,
questions: []
}
a.push(std);
}
std.questions.push({
question: c.questions,
answer: c.answer,
time: c.time
});
return a;
}, []);
console.log(result);