2016-05-18 58 views
-2

Javascript新手在這裏:)我想實現......但不能弄明白。創建一個沒有任何重複數組的數組Javascript

這是我的數據。

<pre> 
[ 
    [ 
    { 
     "id": 2759178563, 
     "title": "Ergonomic Paper Computer", 
     "handle": "ergonomic-paper-computer", 
     "body_html": "Enable turn-key infrastructures", 
     "published_at": "2015-09-23T20:51:49-04:00", 
     "created_at": "2015-09-23T20:51:49-04:00", 
     "updated_at": "2015-09-23T20:51:49-04:00", 
     "vendor": "Schinner Inc", 
     "product_type": "Computer", 
     "tags": [ 
     "Computer", 
     "Ergonomic", 
     "Paper" 
     ], 
     "variants": [ 
     { 
      "id": 8041863299, 
      "title": "White", 
      "option1": "White", 
      "option2": null, 
      "option3": null, 
      "price": "67.25", 
      "grams": 5145, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 1, 
      "product_id": 2759178563, 
      "created_at": "2015-09-23T20:51:49-04:00", 
      "updated_at": "2015-09-23T20:51:49-04:00", 
      "available": true, 
      "featured_image": null 
     }, 
     { 
      "id": 8041863363, 
      "title": "Mint green", 
      "option1": "Mint green", 
      "option2": null, 
      "option3": null, 
      "price": "29.58", 
      "grams": 6860, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 2, 
      "product_id": 2759178563, 
      "created_at": "2015-09-23T20:51:49-04:00", 
      "updated_at": "2015-09-23T20:51:49-04:00", 
      "available": true, 
      "featured_image": null 
     }, 
     { 
      "id": 8041863491, 
      "title": "Yellow", 
      "option1": "Yellow", 
      "option2": null, 
      "option3": null, 
      "price": "54.19", 
      "grams": 6045, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 3, 
      "product_id": 2759178563, 
      "created_at": "2015-09-23T20:51:49-04:00", 
      "updated_at": "2015-09-23T20:51:49-04:00", 
      "available": true, 
      "featured_image": null 
     }, 
     { 
      "id": 8041863555, 
      "title": "Blue", 
      "option1": "Blue", 
      "option2": null, 
      "option3": null, 
      "price": "22.88", 
      "grams": 9526, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 4, 
      "product_id": 2759178563, 
      "created_at": "2015-09-23T20:51:49-04:00", 
      "updated_at": "2015-09-23T20:51:49-04:00", 
      "available": true, 
      "featured_image": null 
     } 
     ], 
     "images": [ 
     { 
      "id": 5642046019, 
      "created_at": "2015-09-23T20:51:49-04:00", 
      "position": 1, 
      "updated_at": "2015-09-23T20:51:49-04:00", 
      "product_id": 2759178563, 
      "variant_ids": [], 
      "src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Ergonomic_20Paper_20Computer.png?v=1443055909" 
     } 
     ], 
     "options": [ 
     { 
      "name": "Title", 
      "position": 1, 
      "values": [ 
      "White", 
      "Mint green", 
      "Yellow", 
      "Blue" 
      ] 
     } 
     ] 
    }, 
    { 
     "id": 2759192387, 
     "title": "Heavy Duty Concrete Keyboard", 
     "handle": "heavy-duty-concrete-keyboard", 
     "body_html": "Strategize synergistic e-markets", 
     "published_at": "2015-09-23T20:52:07-04:00", 
     "created_at": "2015-09-23T20:52:07-04:00", 
     "updated_at": "2015-09-23T20:52:07-04:00", 
     "vendor": "Stiedemann and Sons", 
     "product_type": "Keyboard", 
     "tags": [ 
     "Concrete", 
     "Duty", 
     "Heavy", 
     "Keyboard" 
     ], 
     "variants": [ 
     { 
      "id": 8041883779, 
      "title": "Magenta", 
      "option1": "Magenta", 
      "option2": null, 
      "option3": null, 
      "price": "14.20", 
      "grams": 3030, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 1, 
      "product_id": 2759192387, 
      "created_at": "2015-09-23T20:52:07-04:00", 
      "updated_at": "2015-09-23T20:52:07-04:00", 
      "available": true, 
      "featured_image": null 
     } 
     ], 
     "images": [ 
     { 
      "id": 5642056899, 
      "created_at": "2015-09-23T20:52:07-04:00", 
      "position": 1, 
      "updated_at": "2015-09-23T20:52:07-04:00", 
      "product_id": 2759192387, 
      "variant_ids": [], 
      "src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Heavy_20Duty_20Concrete_20Keyboard.png?v=1443055927" 
     } 
     ], 
     "options": [ 
     { 
      "name": "Title", 
      "position": 1, 
      "values": [ 
      "Magenta" 
      ] 
     } 
     ] 
    } 
    ], 
    [ 
    { 
     "id": 2759168323, 
     "title": "Awesome Cotton Computer", 
     "handle": "awesome-cotton-computer-1", 
     "body_html": "Brand synergistic applications", 
     "published_at": "2015-09-23T20:51:24-04:00", 
     "created_at": "2015-09-23T20:51:24-04:00", 
     "updated_at": "2015-09-23T20:51:24-04:00", 
     "vendor": "Hills Group", 
     "product_type": "Computer", 
     "tags": [ 
     "Awesome", 
     "Computer", 
     "Cotton" 
     ], 
     "variants": [ 
     { 
      "id": 8041841795, 
      "title": "Black", 
      "option1": "Black", 
      "option2": null, 
      "option3": null, 
      "price": "2.05", 
      "grams": 1906, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 1, 
      "product_id": 2759168323, 
      "created_at": "2015-09-23T20:51:24-04:00", 
      "updated_at": "2015-09-23T20:51:24-04:00", 
      "available": true, 
      "featured_image": null 
     }, 
     { 
      "id": 8041841859, 
      "title": "Orchid", 
      "option1": "Orchid", 
      "option2": null, 
      "option3": null, 
      "price": "10.78", 
      "grams": 4970, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 2, 
      "product_id": 2759168323, 
      "created_at": "2015-09-23T20:51:24-04:00", 
      "updated_at": "2015-09-23T20:51:24-04:00", 
      "available": true, 
      "featured_image": null 
     }, 
     { 
      "id": 8041841923, 
      "title": "Tan", 
      "option1": "Tan", 
      "option2": null, 
      "option3": null, 
      "price": "50.54", 
      "grams": 6738, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 3, 
      "product_id": 2759168323, 
      "created_at": "2015-09-23T20:51:24-04:00", 
      "updated_at": "2015-09-23T20:51:24-04:00", 
      "available": true, 
      "featured_image": null 
     }, 
     { 
      "id": 8041841987, 
      "title": "Teal", 
      "option1": "Teal", 
      "option2": null, 
      "option3": null, 
      "price": "91.51", 
      "grams": 8718, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 4, 
      "product_id": 2759168323, 
      "created_at": "2015-09-23T20:51:24-04:00", 
      "updated_at": "2015-09-23T20:51:24-04:00", 
      "available": true, 
      "featured_image": null 
     }, 
     { 
      "id": 8041842051, 
      "title": "Gold", 
      "option1": "Gold", 
      "option2": null, 
      "option3": null, 
      "price": "8.24", 
      "grams": 194, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 5, 
      "product_id": 2759168323, 
      "created_at": "2015-09-23T20:51:24-04:00", 
      "updated_at": "2015-09-23T20:51:24-04:00", 
      "available": true, 
      "featured_image": null 
     } 
     ], 
     "images": [ 
     { 
      "id": 5642032131, 
      "created_at": "2015-09-23T20:51:24-04:00", 
      "position": 1, 
      "updated_at": "2015-09-23T20:51:24-04:00", 
      "product_id": 2759168323, 
      "variant_ids": [], 
      "src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Awesome_20Cotton_20Computer_f74fc2a4-efa5-42ca-a3b2-36c378f1c003.png?v=1443055884" 
     } 
     ], 
     "options": [ 
     { 
      "name": "Title", 
      "position": 1, 
      "values": [ 
      "Black", 
      "Orchid", 
      "Tan", 
      "Teal", 
      "Gold" 
      ] 
     } 
     ] 
    }, 
    { 
     "id": 2759192387, 
     "title": "Heavy Duty Concrete Keyboard", 
     "handle": "heavy-duty-concrete-keyboard", 
     "body_html": "Strategize synergistic e-markets", 
     "published_at": "2015-09-23T20:52:07-04:00", 
     "created_at": "2015-09-23T20:52:07-04:00", 
     "updated_at": "2015-09-23T20:52:07-04:00", 
     "vendor": "Stiedemann and Sons", 
     "product_type": "Keyboard", 
     "tags": [ 
     "Concrete", 
     "Duty", 
     "Heavy", 
     "Keyboard" 
     ], 
     "variants": [ 
     { 
      "id": 8041883779, 
      "title": "Magenta", 
      "option1": "Magenta", 
      "option2": null, 
      "option3": null, 
      "price": "14.20", 
      "grams": 3030, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 1, 
      "product_id": 2759192387, 
      "created_at": "2015-09-23T20:52:07-04:00", 
      "updated_at": "2015-09-23T20:52:07-04:00", 
      "available": true, 
      "featured_image": null 
     } 
     ], 
     "images": [ 
     { 
      "id": 5642056899, 
      "created_at": "2015-09-23T20:52:07-04:00", 
      "position": 1, 
      "updated_at": "2015-09-23T20:52:07-04:00", 
      "product_id": 2759192387, 
      "variant_ids": [], 
      "src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Heavy_20Duty_20Concrete_20Keyboard.png?v=1443055927" 
     } 
     ], 
     "options": [ 
     { 
      "name": "Title", 
      "position": 1, 
      "values": [ 
      "Magenta" 
      ] 
     } 
     ] 
    } 
    ], 
    [ 
    { 
     "id": 2759167747, 
     "title": "Awesome Bronze Computer", 
     "handle": "awesome-bronze-computer", 
     "body_html": "Orchestrate holistic web services", 
     "published_at": "2015-09-23T20:51:20-04:00", 
     "created_at": "2015-09-23T20:51:20-04:00", 
     "updated_at": "2015-09-23T20:51:20-04:00", 
     "vendor": "Zboncak-Kemmer", 
     "product_type": "Computer", 
     "tags": [ 
     "Awesome", 
     "Bronze", 
     "Computer" 
     ], 
     "variants": [ 
     { 
      "id": 8041840195, 
      "title": "Magenta", 
      "option1": "Magenta", 
      "option2": null, 
      "option3": null, 
      "price": "56.69", 
      "grams": 6035, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 1, 
      "product_id": 2759167747, 
      "created_at": "2015-09-23T20:51:20-04:00", 
      "updated_at": "2015-09-23T20:51:20-04:00", 
      "available": true, 
      "featured_image": null 
     }, 
     { 
      "id": 8041840259, 
      "title": "Mint green", 
      "option1": "Mint green", 
      "option2": null, 
      "option3": null, 
      "price": "63.89", 
      "grams": 9961, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 2, 
      "product_id": 2759167747, 
      "created_at": "2015-09-23T20:51:20-04:00", 
      "updated_at": "2015-09-23T20:51:20-04:00", 
      "available": true, 
      "featured_image": null 
     }, 
     { 
      "id": 8041840323, 
      "title": "Maroon", 
      "option1": "Maroon", 
      "option2": null, 
      "option3": null, 
      "price": "18.76", 
      "grams": 922, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 3, 
      "product_id": 2759167747, 
      "created_at": "2015-09-23T20:51:20-04:00", 
      "updated_at": "2015-09-23T20:51:20-04:00", 
      "available": true, 
      "featured_image": null 
     }, 
     { 
      "id": 8041840387, 
      "title": "Salmon", 
      "option1": "Salmon", 
      "option2": null, 
      "option3": null, 
      "price": "97.60", 
      "grams": 1355, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 4, 
      "product_id": 2759167747, 
      "created_at": "2015-09-23T20:51:20-04:00", 
      "updated_at": "2015-09-23T20:51:20-04:00", 
      "available": true, 
      "featured_image": null 
     } 
     ], 
     "images": [ 
     { 
      "id": 5642031235, 
      "created_at": "2015-09-23T20:51:20-04:00", 
      "position": 1, 
      "updated_at": "2015-09-23T20:51:20-04:00", 
      "product_id": 2759167747, 
      "variant_ids": [], 
      "src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Awesome_20Bronze_20Computer.png?v=1443055880" 
     } 
     ], 
     "options": [ 
     { 
      "name": "Title", 
      "position": 1, 
      "values": [ 
      "Magenta", 
      "Mint green", 
      "Maroon", 
      "Salmon" 
      ] 
     } 
     ] 
    }, 
    { 
     "id": 2759192387, 
     "title": "Heavy Duty Concrete Keyboard", 
     "handle": "heavy-duty-concrete-keyboard", 
     "body_html": "Strategize synergistic e-markets", 
     "published_at": "2015-09-23T20:52:07-04:00", 
     "created_at": "2015-09-23T20:52:07-04:00", 
     "updated_at": "2015-09-23T20:52:07-04:00", 
     "vendor": "Stiedemann and Sons", 
     "product_type": "Keyboard", 
     "tags": [ 
     "Concrete", 
     "Duty", 
     "Heavy", 
     "Keyboard" 
     ], 
     "variants": [ 
     { 
      "id": 8041883779, 
      "title": "Magenta", 
      "option1": "Magenta", 
      "option2": null, 
      "option3": null, 
      "price": "14.20", 
      "grams": 3030, 
      "compare_at_price": null, 
      "sku": "", 
      "requires_shipping": true, 
      "taxable": true, 
      "position": 1, 
      "product_id": 2759192387, 
      "created_at": "2015-09-23T20:52:07-04:00", 
      "updated_at": "2015-09-23T20:52:07-04:00", 
      "available": true, 
      "featured_image": null 
     } 
     ], 
     "images": [ 
     { 
      "id": 5642056899, 
      "created_at": "2015-09-23T20:52:07-04:00", 
      "position": 1, 
      "updated_at": "2015-09-23T20:52:07-04:00", 
      "product_id": 2759192387, 
      "variant_ids": [], 
      "src": "https://cdn.shopify.com/s/files/1/1000/7970/products/Heavy_20Duty_20Concrete_20Keyboard.png?v=1443055927" 
     } 
     ], 
     "options": [ 
     { 
      "name": "Title", 
      "position": 1, 
      "values": [ 
      "Magenta" 
      ] 
     } 
     ] 
    } 
    ] 
] 
</pre> 

這裏是我的代碼:

import { Injectable } from '@angular/core'; 
import { Http, Response } from '@angular/http'; 

@Injectable() 

export class ShopifyService { 

    /** 
    * Initialization 
    */ 
    constructor (
     private http: Http 
    ) {} 

    private shopifyUrl = 'app/heroes.json'; 

    errorMessage: string; 

    /** 
    * Private Methods 
    */ 
    fetchProducts(): Promise<Response> { 
     return this.http.get(this.shopifyUrl) 
      .toPromise() 
      .then(this.extractData) 
      .catch(this.handleError); 
    } 

    private extractData(res: Response) { 
     if (res.status < 200 || res.status >= 300) { 
      throw new Error('Bad response status: ' + res.status); 
     } 
     let body = res.json(); 
     if (body.data) { 
      return body.data; 
     } 
     else if (body.products) { 
      return body.products; 
     } 
     else { 
      return { }; 
     } 
    } 

    private handleError (error: any) { 
     let errMsg = error.message || 'Server error'; 
     console.error(errMsg); 
     return Promise.reject(errMsg); 
    } 

    private filterProducts(list, archetype) { 
     var result = []; 

     for (var i = 0; i < list.length; i++) { 
      if (list[i].product_type === archetype) { 
       result.push(list[i]); 
      } 
     } 
     return result; 
    } 

    /** 
    * Public Methods 
    */ 
    public findProducts(archetypes) { 
     return this.fetchProducts().then(
       products => { 
         var result = [], 
          fetchedResponse = []; 

        for (var i = 0; i < archetypes.length; i++) { 
         fetchedResponse = this.filterProducts(
          products, archetypes[i] 
         ); 
         result = result.concat(fetchedResponse); 
        } 
        return result; 
       }, 
       error => this.errorMessage = <any>error 
      ) 
    } 

    public findSingleProductVariant() { 
     var result = [], 
      j = 0; 

     return this.findVariants().then(
      product => { 
       for (var i = 0; i < product.length; i++) { 
        // console.log(product[i]); 
       } 
      } 
     ); 
     // return result; 
    } 

    findVariants() { 
     return this.findProducts(['Computer', 'Keyboard']).then(
      products => { 
       var result = []; 

       for (var i = 0; i < products.length; i++) { 
        if (products[i].product_type === 'Computer') { 
         for (var j = 0; j < products.length; j++) { 
          if (products[j].product_type === 'Keyboard') { 
           result.push([products[i], products[j]]); 
          } 
         } 
        } 
       } 
       console.log(result); 
       return result; 
      } 
     ) 
    } 
} 

直到我在這裏得到了什麼樣的一半。一組二維數組(其中一個來自鍵盤類型,另一個來自計算機類型)。

它是某事像這樣==>

[ 
    [ 
     [Computer_A], 
     [Keyboard_A] 
    ], 
    [ 
     [Computer_B], 
     [Keyboard_A] 
    ], 
    [ 
     [Computer_C], 
     [Keyboard_A] 
    ], 
] 

現在這些對象中的每一個具有其自己的變體的陣列。我必須修改我的findSingleProductVariant以便遍歷這些陣列中的每一個,選擇一個keyboard變體(其陣列有一個價格字段),也可以選擇一個computer變體並計算價格。如果價格低於1000,它可以繼續選擇另一個鍵盤和計算機。但問題是它不能有兩個相同的數組。比如我們不能這樣做。

[ 
    ['Computer_A'], ['Keyboard_A'], 
    ['Computer_A'], ['Keyboard_A'], 
] 
+0

請問您能否以**文本形式**將相關數據添加到問題中? –

+0

一些字符串中的數字與桌子或燈具有什麼關係? –

+0

抱歉混淆妮娜:)在我的代碼中,我首先篩選某些產品類型,然後對它們進行篩選,我只對它們的價格感興趣。我會在問題中提供一部分真實數據。感謝您抽出時間 –

回答

-1

這裏關鍵的是你不能直接比較對象並期待相關的結果,例如,

['Red Lamp', 'Blue Table'] === ['Red Lamp', 'Blue Table'] 

將返回false(Array.indexOf()如預期,因爲它使用了嚴格的比較也將無法正常工作)。因此對於一個數組數組,您將不得不使用兩個嵌套循環來比較原始值。

+0

謝謝卡瑪。嵌套循環幫助我很多進一步移動,但我仍然卡住,但在另一部分:) –

相關問題