Advertisement
Guest User

iterating through object

a guest
Apr 23rd, 2018
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const business = [
  2.     {
  3.         id: 1,
  4.         name: "Emcee Enterprises",
  5.         location: [
  6.                         {
  7.                             id: 1,
  8.                             locate: [],
  9.                         businesses: []
  10.                 },
  11.                         {
  12.                             id: 2,
  13.                             locate: [],
  14.                         businesses: []
  15.                 },
  16.              
  17.              
  18.         ]    
  19.         ,
  20.         category: "IT Services",
  21.         phone: "08074037215",
  22.         email: 'u.ezeh@yahoo.com',
  23.         reviews: []
  24.     },
  25.     {
  26.         id: 2,
  27.         name: "Macuval Enterprises",
  28.         location: [
  29.             {
  30.                 id: 1,
  31.                 locate: [],
  32.             businesses: []
  33.     },
  34.             {
  35.                 id: 2,
  36.                 locate: [],
  37.             businesses: []
  38.     },
  39.  
  40.  
  41. ]    
  42. ,
  43.         category: "Entertainment Services",
  44.         phone:"08074037215",
  45.         email: 'mac.val@yahoo.com',
  46.         reviews: []
  47.     },
  48.     {
  49.         id: 3,
  50.         name: "Zulu Enterprises",
  51.         location: [
  52.             {
  53.                 id: 1,
  54.                 locate: [],
  55.             businesses: []
  56.     },
  57.             {
  58.                 id: 2,
  59.                 locate: [],
  60.             businesses: []
  61.     },
  62.  
  63.  
  64. ]    
  65. ,
  66.         category: "IT Services",
  67.         phone: "08064039014",
  68.         email: 'zulu@yahoo.com',
  69.         reviews: []
  70.     },
  71. ]
  72. const newLocation = []
  73. let key;
  74. let category = 'IT Services'
  75.  
  76. const sample = () => {
  77.  
  78. for(key in business){
  79.     console.log(business[key].category)
  80.     // console.log(category)
  81.     if (business[key].category === category){
  82.         newLocation.push(business[key])
  83.         // return newLocation
  84.     }else{
  85.         return 'Category is not Found'
  86.     }
  87.     // return newLocation  
  88. }
  89.  return newLocation
  90. }
  91.  
  92. console.log(sample());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement