1

So I have an array of names:

const names = ['student1', 'student2', 'student3'] 

and I have an array of attendance objects:

const attendance = [ {student1: ['On Time', 'Late']}, {student2: ['Late', 'Late']}, {student3: ['On Time', 'Excused']}, ] 

I wanted to find the student object in the attendance array based off the names from the names array.

So currently I have:

names.forEach(person => { function attendanceData(p) { return Object.keys(attendance).toString() == p.toString() } console.log(attendance.find(attendanceData(person))) }) 

However, this gives me an error saying:

Uncaught (in promise) TypeError: false is not a function 

The next stack says "at Array.find()"

I'm wondering how I'm not using this correctly and if there was a better way to do this, what should I do?

3
  • what do you need this for?
    – richytong
    CommentedMay 28, 2020 at 22:37
  • The result of Object.keys(attendance).toString() will be "0,1,2"
    – Phil
    CommentedMay 28, 2020 at 22:41
  • Your data structure does not lend itself to being consumed very well. A better structure for attendance would be {name: 'student', attendance: [...]}. That way you don't need to guess at random keys
    – Phil
    CommentedMay 28, 2020 at 22:43

2 Answers 2

2

I believe this is what you want. Your data is structured a little strangely though, so I would like to know on a larger scale what you want this code to do.

const findStudentAttendance = (att, studentName) => { return att.find(obj => Object.keys(obj)[0] === studentName) } names.forEach(name => { console.log( findStudentAttendance(attendance, name) ) }) /* => { student1: [ 'On Time', 'Late' ] } { student2: [ 'Late', 'Late' ] } { student3: [ 'On Time', 'Excused' ] } */ 
1
  • 1
    This worked wonderfully. Thank you very much. Also to answer your question, I'm just building a little app that builds a pdf of student grades and attendanceCommentedMay 28, 2020 at 23:13
0

try it

let result = attendance.filter(obj => names.includes(Object.keys(obj)[0])) console.log(result) 

Use array's method "filter" for getting items. You need get keys of object and take first key. And then you'll can check presence in names array, via array's method "includes"

more information about this methods here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Filter

1
  • 3
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation.CommentedMay 29, 2020 at 2:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.