SHARE
TWEET

Untitled

a guest Aug 17th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. searchAndSort(){
  2.         let searchData = [
  3.             {productName: 'Cheetos', category: 'Meciners'},
  4.             {productName: 'Chitato', category: 'Meciners'},
  5.             {productName: 'Kitkat', category: 'Chocolate'},
  6.             {productName: 'Catburry', category: 'Chocolate'},
  7.             {productName: 'Susuku', category: 'Kids Snack'},
  8.             {productName: 'Yupi', category: 'Kids Snack'},
  9.             {productName: 'Vodka', category: 'Adult Drink'},
  10.         ];
  11.  
  12.         // *THIS IS SEARCH by productName
  13.         console.log("*THIS IS SEARCH by productName");
  14.  
  15.             let searchTemp = [];
  16.             let seachInput = 'ch'; //USER INPUT, CHANGE THIS IF YOU WANT
  17.             let inputLength = seachInput.length;
  18.  
  19.  
  20.             // UNTUK PRODUCT AME
  21.             for(let index=0; index < searchData.length; index++){
  22.                
  23.                 let productName = searchData[index].productName;
  24.                 // console.log("ProductName: ", searchData[index].productName);
  25.                 // console.log("substr: ", productName.substr(0, 2));
  26.  
  27.                 for(let index2=0; index2 < productName.length; index2++){
  28.  
  29.                     let cacahHuruf = productName.substr(index2, inputLength).toLocaleLowerCase();
  30.                     console.log(`apakah seachInput [${seachInput}] === [${cacahHuruf}]`);
  31.  
  32.                     if(seachInput === cacahHuruf){
  33.                         console.log(`===SAMA: [${productName}] masuk ke temp`);
  34.                         searchTemp[searchTemp.length] = searchData[index];
  35.                     }
  36.  
  37.                 } //END OF INDEX2
  38.  
  39.             } //END OF INDEX
  40.            
  41.  
  42.             //UNTUK CATEGORY
  43.             for(let index=0; index < searchData.length; index++){
  44.                
  45.                 let category = searchData[index].category;
  46.                 // console.log("category: ", searchData[index].category);
  47.                 // console.log("substr: ", category.substr(0, 2));
  48.  
  49.                 for(let index2=0; index2 < category.length; index2++){
  50.  
  51.                     let cacahHuruf = category.substr(index2, inputLength).toLocaleLowerCase();
  52.                     // console.log(`apakah seachInput [${seachInput}] === [${cacahHuruf}]`);
  53.  
  54.                     if(seachInput === cacahHuruf){
  55.                         // console.log(`===SAMA: [${category}] masuk ke temp`);
  56.                         searchTemp[searchTemp.length] = searchData[index];
  57.                     }
  58.  
  59.                 } //END OF INDEX2
  60.  
  61.             } //END OF INDEX
  62.  
  63.         console.log("===searchTemp: ", searchTemp);
  64.         console.log("*END OF SEARCH");
  65.         // END OF SEARCH
  66.  
  67.  
  68.  
  69.         // *SORT HERE
  70.         console.log("*SORT HERE");
  71.  
  72.         searchData.sort((prev, next) => (prev.productName > next.productName) ? true : false);
  73.         console.log("result: ", searchData);
  74.  
  75.         // *NOTE:
  76.         // (prev.productName > next.productName) kalau kondisi ini bener dan di nilai TRUE maka data di naikin ke atas
  77.  
  78.         console.log("*END OF SHORT");
  79.         // END OF SHORT
  80.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top