Advertisement
ferrynurr

Array Object

Oct 5th, 2020 (edited)
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var now = new Date();
  2. var thisYear = now.getFullYear(); // 2020 (tahun sekarang)
  3.  
  4. function arrayToObject(arr) {
  5.   for (var i = 0; i < arr.length; i++) {
  6.     var umur = 0;
  7.     var no = i + 1;
  8.     if (arr[i][3] == null || arr[i][3] > thisYear) {
  9.       umur = "Invalid Birth Year";
  10.     } else {
  11.       umur = thisYear - arr[i][3];
  12.     }
  13.  
  14.     var arrObject = {
  15.       firstName: arr[i][0],
  16.       lastName: arr[i][1],
  17.       gender: arr[i][2],
  18.       age: umur,
  19.     };
  20.     console.log(
  21.       no + ". " + arrObject.firstName + " " + arrObject.lastName + ": "
  22.     );
  23.     console.log(arrObject);
  24.   }
  25. }
  26.  
  27. var people = [
  28.   ["Bruce", "Banner", "male", 1975],
  29.   ["Natasha", "Romanoff", "female"],
  30. ];
  31. arrayToObject(people);
  32. console.log();
  33. var people2 = [
  34.   ["Tony", "Stark", "male", 1980],
  35.   ["Pepper", "Pots", "female", 2023],
  36. ];
  37. arrayToObject(people2);
  38. arrayToObject([]);
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement