javascript - 通过使用嵌套数组循环遍历对象数组来创建对象。Javascript
问题描述
我在遍历数组内的数组和数组内的对象并将这些名称推入我的结果数组时遇到问题。我知道如何在我的结果对象中创建新属性,但为了做到这一点,我需要将名称推送到我创建的名称数组中。也许我没有正确循环它。
var students = [{
name: 'joe',
color: 'blue',
age: 13,
height: 61,
"favorite hobbies": ['drawing', 'sports', 'swimming'],
birthday: '1/12/1992'
},
{
name: 'dave',
color: 'red',
age: 14,
height: 60,
"favorite hobbies": ['swimming', 'biking', 'hiking'],
birthday: 'January 29, 1992'
},
{
name: 'sally',
color: 'yellow',
age: 13,
height: 64,
"favorite hobbies": ['biking', 'singing', 'dancing']
},
{
name: 'jane',
color: 'white',
age: 12,
height: 58,
"favorite hobbies": ['dancing', 'swimming', 'drawing']
},
{
name: 'kayla',
color: 'blue',
age: 14,
height: 62,
"favorite hobbies": ['hiking', 'sports', 'drawing']
},
];
// findHobbies, given an array of student objects, returns:
// an object containing three properties:
// hobby
// the targeted hobby
// students
// array of student names that match target hobby
// averageAge
// average age of students that match target hobby
var findHobbies = function(arr, target) {
var result = {};
result.hobby = target;
// result.averageAge =
for (var i = 0; i < arr.length; i++) {
var names = [];
for (var k = 0; k < arr[i]['favorite hobbies'].length; k++) {
// console.log(arr[i]['favorite hobbies'][k])
if (arr[i]['favorite hobbies'][k] === target) {
array.push(arr[i].name);
}
console.log(array)
}
}
}
console.log(findHobbies(students, 'swimming'));
/*
should return:
{
hobby: 'swimming',
students: ['joe', 'dave', 'jane'],
averageAge: 13
}
*/
解决方案
对于替代解决方案,您可以使用reduce来实现结果
var students = [
{
name: "joe",
color: "blue",
age: 13,
height: 61,
"favorite hobbies": ["drawing", "sports", "swimming"],
birthday: "1/12/1992",
},
{
name: "dave",
color: "red",
age: 14,
height: 60,
"favorite hobbies": ["swimming", "biking", "hiking"],
birthday: "January 29, 1992",
},
{
name: "sally",
color: "yellow",
age: 13,
height: 64,
"favorite hobbies": ["biking", "singing", "dancing"],
},
{
name: "jane",
color: "white",
age: 12,
height: 58,
"favorite hobbies": ["dancing", "swimming", "drawing"],
},
{
name: "kayla",
color: "blue",
age: 14,
height: 62,
"favorite hobbies": ["hiking", "sports", "drawing"],
},
];
// findHobbies, given an array of student objects, returns:
// an object containing three properties:
// hobby
// the targeted hobby
// students
// array of student names that match target hobby
// averageAge
// average age of students that match target hobby
function findHobbies(arr, hobby) {
const accumulator = {
hobby,
students: [],
averageAge: 0,
};
const result = students.reduce((acc, curr) => {
const { name, age, "favorite hobbies": sHobby } = curr;
if (sHobby.includes(hobby)) {
acc.students.push(name);
acc.averageAge += age;
}
return acc;
}, accumulator);
result.averageAge /= result.students.length;
return result;
}
console.log(findHobbies(students, "swimming"));
推荐阅读
- excel - 如何使用计时器触发器在 azure function App 中创建 excel 文件
- c++ - 构建 CXX 对象 CMakeFiles 错误:ProductCategory *result = 0;未在范围内声明(SaleItem 有 Product Category Pointer 作为成员)
- django - Django 定义加载的 url 和显示的 temolate
- wordpress - Nginx - 将 Wordpress 作为子域运行,另一个应用程序在主域上运行
- reactjs - React Native 如何实现这个模型?从照片中反应原生作物
- android - 无法读取在微调器 Android 中选择的国家/地区代码
- lua - Roblox Lua 试图补间部分以像平台一样向后移动和第四移动
- botframework - Offline-directline:我的网络聊天应用程序不断向 /directline/conversations/ 端点发送 GET 请求。这是正常的吗?
- laravel - Laravel Auth 每个子域都有不同的连接?
- laravel - Laravel Varbox 2.x - 包含关系的重复模型