给定对象数组:
var teachers = [
{
FullName: "Mark Jones",
Address: "123 Main Street",
Students: {
Monday: {
Stone: ["8:30", "10:30"],
Cameron: [" ", " "],
Julia: [" ", " "],
Zoe: ["3:30", "5:30"]
},
Tuesday: {
Jordan: ["12:00", "1:00"],
Hal: [" ", " "],
Kiko: [" ", " "]
}
}
},
{
FullName: "Skip Roberts",
Address: "123 Main Street",
Students: {
Monday: {
Hal: ["9:30", "10:30"],
Hana: [" ", " "],
Ron: [" ", " "],
Lola: ["4:30", "5:30"]
},
Tuesday: {
Josh: ["11:00", "12:00"],
George: [" ", " "],
Paula: ["5:00", "6:00"]
}
}
}
]
我想要检索所有等于[“”,“”]的值的所有键(学生姓名)。我尝试了以下但它只提供了第一个键并跳过了其余的(这里我得到星期一的键:
var array = [];
teachers.forEach(function (doc) {
array.push(_.findKey(doc.Students.Monday, [" ", " "]));
});
console.log(array);
输出:
["Cameron", "Hana"]
我想要:
["Cameron", "Julia", "Hana", "Ron"]
答案 0 :(得分:2)
这是使用下划线的解决方案:
var result = _.chain(teachers)
.pluck('Students')
.map(day => _.pick(day.Monday, times => times[0] == ' ' && times[1] == ' '))
.flatten()
.map(_.keys)
.flatten()
.value()
答案 1 :(得分:1)
以下是使用.reduce()
和.filter()
方法在vanilla JS中的一种可能方式。你也可以使用他们的下划线对应物。
var teachers = [{
FullName: "Mark Jones",
Address: "123 Main Street",
Students: {
Monday : { Stone: ["8:30", "10:30"], Cameron: [" ", " "], Julia: [" ", " "], Zoe: ["3:30", "5:30"] },
Tuesday: { Jordan: ["12:00", "1:00"], Hal: [" ", " "], Kiko: [" ", " "] }
}
}, {
FullName: "Skip Roberts",
Address: "123 Main Street",
Students: {
Monday : { Hal: ["9:30", "10:30"], Hana: [" ", " "], Ron: [" ", " "], Lola: ["4:30", "5:30"] },
Tuesday: { Josh: ["11:00", "12:00"], George: [" ", " "], Paula: ["5:00", "6:00"] }
}
}];
function findStudents(day) {
return teachers.reduce(function(res, cur) {
var student = cur.Students[day];
return res.concat(Object.keys(student).filter(function(k) {
return (student[k][0] == " " && student[k][1] == " ");
}));
}, []);
}
var res = findStudents("Monday");
console.log(res);

答案 2 :(得分:1)
这是两个本机JavaScript(ES6)函数,一个用于给定的日期,一个用于任何一天。他们还确保名称仅列出一次:
function getStudentsForDay(teachers, day) {
return [...teachers.reduce( (col, teacher) =>
Object.keys(teacher.Students[day]).reduce(
(col, name) => teacher.Students[day][name].every(time => time == ' ')
? col.add(name) : col,
col
), new Set()
)];
}
function getStudentsForAnyDay(teachers) {
return [...teachers.reduce( (col, teacher) =>
Object.keys(teacher.Students).reduce( (col, day) =>
Object.keys(teacher.Students[day]).reduce(
(col, name) => teacher.Students[day][name].every(time => time == ' ')
? col.add(name) : col,
col
), col
), new Set()
)];
}
var teachers = [
{
FullName: "Mark Jones",
Address: "123 Main Street",
Students: {
Monday: {
Stone: ["8:30", "10:30"],
Cameron: [" ", " "],
Julia: [" ", " "],
Zoe: ["3:30", "5:30"]
},
Tuesday: {
Jordan: ["12:00", "1:00"],
Hal: [" ", " "],
Kiko: [" ", " "]
}
}
},
{
FullName: "Skip Roberts",
Address: "123 Main Street",
Students: {
Monday: {
Hal: ["9:30", "10:30"],
Hana: [" ", " "],
Ron: [" ", " "],
Lola: ["4:30", "5:30"]
},
Tuesday: {
Josh: ["11:00", "12:00"],
George: [" ", " "],
Paula: ["5:00", "6:00"]
}
}
}
];
console.log('For Monday: ', getStudentsForDay(teachers, 'Monday'));
console.log('For any day: ', getStudentsForAnyDay(teachers));

答案 3 :(得分:1)
问题是_.findKey
只检索第一个键,并且似乎没有任何函数可以检索所有这些键。
如果您不是在寻找下划线解决方案,可以使用类似这样的函数在vanilla JavaScript中执行此操作:
var arr = [];
function lookForEmpty(obj){
for(var k in obj){
var v = obj[k];
if(v instanceof Array){
if(v.length == 2 && v[0] == " " && v[1] == " "){
arr.push(k);
}
}else if(v instanceof Object){
lookForEmpty(v);
}
}
}
lookForEmpty(teachers);