Advertisement
Guest User

Untitled

a guest
May 4th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. function innerJoin(){
  2. // Create array to return
  3. var arrToReturn = [];
  4.  
  5. // Loop over usersHaveBooks
  6. for(var recordIdx = 0; recordIdx < usersHaveBooks.length; recordIdx += 1){
  7. // Creating miniArr that will hold record obj, userobj, and bookobj
  8. // initially just put the record obj we have access to inside
  9. var miniArr = [usersHaveBooks[recordIdx]]
  10.  
  11. // Find correct user object
  12. var correctUserObj = findById(usersHaveBooks[recordIdx].user_id, users)
  13.  
  14. // Find correct book object
  15. var correctBookObj = findById(usersHaveBooks[recordIdx].book_id, books)
  16.  
  17. // Put user obj in miniArr
  18. miniArr.push(correctUserObj)
  19. // Put book obj in miniArr
  20. miniArr.push(correctBookObj)
  21.  
  22. // ALTERNATE CODE CAN TAKE PLACE OF EVERYTHING ABOVE
  23. // var miniArr = [usersHaveBooks[recordIdx],findById(usersHaveBooks[recordIdx].user_id, users),findById(usersHaveBooks[recordIdx].book_id, books)];
  24.  
  25. // If we're here, miniArr is completed and we can add to arrToReturn
  26. arrToReturn.push(miniArr)
  27. }
  28.  
  29. return arrToReturn
  30. }
  31.  
  32. function findById(providedId, records){
  33. // Use the id passed in to find the matching object in record
  34. for(var searchIdx = 0; searchIdx < records.length; searchIdx+=1){
  35. if ( records[searchIdx].id === providedId){
  36. return records[searchIdx];
  37. }
  38. }
  39. // If we get here, no record with providedId as id property
  40. return {error: "No record found"}
  41.  
  42. }
  43.  
  44. // JACK'S BETTER IDEA:
  45. // Create two objects:
  46. // Users object, Books object
  47. // {
  48. // 1: {object from array}
  49. // }
  50.  
  51. console.dir(innerJoin())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement