這是我的第一篇文章,所以我會盡力做到盡可能徹底!我需要將具有相同值的多個 JavaScript 物件轉換為一個唯一的物件,每個物件中的所有其他鍵的總數。
我有一個包含不同學校物件的陣列,每個物件都有一個表示學校學生人數的值。這是我正在使用的內容:
let schools = [
{
school: "a",
students: 1000,
},
{
school: "b",
students: 10000,
},
{
school: "b",
students: 400,
},
{
school: "c",
students: 10000,
},
{
school: "d",
students: 2000,
},
{
school: "c",
students: 1000,
},
{
school: "d",
students: 4000,
},
]
我想回傳每個給定學校的單個物件陣列,以及每個給定學校物件中學生的值的總和。這是我期待的結果:
{
school: "a",
students: 1000,
},
{
school: "b",
students: 10400,
},
{
school: "c",
students: 11000,
},
{
school: "d",
students: 6000,
}
]
這是我的解決方案到目前為止的樣子:
let listOfUniqueSchools = [];
for (let i = 0; i < schools.length; i ) {
if (!listOfUniqueSchools.includes(schools[i].school)) {
listOfUniqueSchools.push(schools[i].school);
} else {
}
}
let outputArray = [];
for (let j = 0; j < listOfUniqueSchools.length; j ) {
for (let k = 0; k < schools.length; k ) {
let totalNumberOfStudents = 0;
if (listOfUniqueSchools[j] == schools[k].school) {
totalNumberOfStudents = schools[k].students;
outputArray.push({
school: schools[k].school,
students: totalNumberOfStudents,
});
}
}
}
這是我得到的:
[
{
school: "a",
students: 1000,
},
{
school: "b",
students: 10000,
},
{
school: "b",
students: 400,
},
{
school: "c",
students: 10000,
},
{
school: "c",
students: 1000,
},
{
school: "d",
students: 2000,
},
{
school: "d",
students: 4000,
},
]
有誰知道我需要改變什么來解決這個問題?
uj5u.com熱心網友回復:
此解決方案使用 for 回圈 (forEach) 來填充以學校為鍵、以學生總數為值的新物件。然后我們使用 Object.keys 方法獲取在 totals 物件中找到的學校陣列,并使用 map 函式將資料轉換為預期的輸出格式。
它比嵌套回圈要干凈得多。
const schools = [{
school: "a",
students: 1000,
},
{
school: "b",
students: 10000,
},
{
school: "b",
students: 400,
},
{
school: "c",
students: 10000,
},
{
school: "d",
students: 2000,
},
{
school: "c",
students: 1000,
},
{
school: "d",
students: 4000,
},
]
const totals = {}
schools.forEach((entry) => {
if (totals.hasOwnProperty(entry.school)) {
totals[entry.school] = entry.students
} else {
totals[entry.school] = entry.students
}
})
const formattedTotals = Object.keys(totals).map((school) => ({
school: school,
students: totals[school]
}))
console.log(formattedTotals)
uj5u.com熱心網友回復:
您的代碼中的問題是該Array.push部分在內部回圈中,而它應該在外部回圈中。
outputArray.push({
school: listOfUniqueSchools[j],
students: totalNumberOfStudents,
});
把它移到外面它會起作用。
使用 可能更簡單Array.reduce,請參閱下面的代碼片段。
let schools = [ { school: "a", students: 1000, }, { school: "b", students: 10000, },{ school: "b", students: 400, }, { school: "c", students: 10000, }, { school: "d", students: 2000, }, { school: "c", students: 1000, }, { school: "d", students: 4000, },];
let listOfUniqueSchools = [];
for (let i = 0; i < schools.length; i ) {
if (!listOfUniqueSchools.includes(schools[i].school)) {
listOfUniqueSchools.push(schools[i].school);
} else {
}
}
let outputArray = [];
for (let j = 0; j < listOfUniqueSchools.length; j ) {
let totalNumberOfStudents = 0;
for (let k = 0; k < schools.length; k ) {
if (listOfUniqueSchools[j] == schools[k].school) {
totalNumberOfStudents = schools[k].students;
}
}
outputArray.push({
school: listOfUniqueSchools[j],
students: totalNumberOfStudents,
});
}
console.log(outputArray);
// or simply use array reduce
const result = schools.reduce((acc, item) => {
const sum = acc.find(sum => sum.school === item.school);
sum ? sum.students = item.students : acc.push({school: item.school, students: item.students});
return acc;
}, []);
console.log(result);
uj5u.com熱心網友回復:
我會遍歷學校并創建一個物件,其中鍵是學校(“a”、“b”、“c”等),值是學生的累計數量。
然后回圈遍歷該陣列以創建一個物件陣列,每個物件都有一個鍵“學校”和值,以及一個帶有累積值的鍵“學生”。
let schools = [
{
school: "a",
students: 1000,
},
{
school: "b",
students: 10000,
},
{
school: "b",
students: 400,
},
{
school: "c",
students: 10000,
},
{
school: "d",
students: 2000,
},
{
school: "c",
students: 1000,
},
{
school: "d",
students: 4000,
},
]
let totals = {} // Interim totals
schools.forEach( function(school) {
// console.log(school.students, school.school)
totals[school.school] = ( totals[school.school] ?? 0 ) school.students;
})
let results = [] // Final results
for ( const school in totals ) {
results.push(
{
school: school,
students: totals[school]
}
)
}
console.log(results)
/*
Array(4) [ {…}, {…}, {…}, {…} ]
?0: Object { school: "a", students: 1000 }
?1: Object { school: "b", students: 10400 }
?2: Object { school: "c", students: 11000 }
?3: Object { school: "d", students: 6000 }
?length: 4
*/
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/346641.html
標籤:javascript 数组 目的
