2017-09-15 3 views
0

masterAccessoriesリスト配列内に存在する製品IDを持つオブジェクトの配列内の製品のみを抽出しようとしています。配列形式のrelated_productのリストjavacriptを使用して別の配列にIDが存在するかどうかを比較して新しい配列を作成します

JSON例:

{"product": 
     { 
      "id": 3, 
      "product_name": "Product Name", 
      "sku": "sku_ID", 
      "description": "description", 
      "product_link": "link", 
      "cat_id": "cat-ID", 
      "cat_name": "Cat Name", 
      "related_products": [5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54] 
     } 
    }, 
    {"product": 
     { 
      "id": 4, 
      "product_name": "product_name", 
      "sku": "sku_id", 
      "description": "description", 
      "product_link": "link", 
      "cat_id": "cat-tc", 
      "cat_name": "Cat Name", 
      "related_products": [] 
     } 
    }, 

私は基本的に関連のprodcuts配列に存在するIDを持つすべての製品を見つけようとしています。

関連製品アレイ:

[5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54] 

これまで運と私の試み:

myPromise.then(function (result) { 
     let products = JSON.parse(result); 
     return products; 
    }, function (result) { 
     console.error(result); 
    }).then(function(products){ 
     let productArray = products[0].products; 
     masterProduct += -1; 
     let accesoriesList = Object.values(productArray)[masterProduct]; 
     let masterAccessories = accesoriesList.product.related_products; 
     console.log('Master: ',masterAccessories); 
     var newArray = []; 
     for(let i=0;i<productArray.length;i++){ 
      if(masterAccessories.indexOf(productArray[i].product.id) === -1){ 
       newArray.push(productArray[i]); 
      } 
     } 
     console.log('NewArray: ',newArray); 
     return accesoriesList; 
    })//just another then() that returns true 

がここであれば、コンソールからproductArray変数から返されるオブジェクトの一つであります助けてください:

1:product:{id: 2, product_name: "Product Name", sku: "SkuID", description: "Description", product_link: "link", …} __proto__:Object 

enter image description here

ご協力いただきありがとうございます。ありがとうございます。

+0

私はすべての製品からあなただけのIDが製品のいずれかの関連製品の配列であるそれらを取得したいことを正しく理解していますか? – Thijs

答えて

0

私はちょうどに等しい-1の代わりに、1

問題領域と等しくないと言うために必要な:

for(let i=0;i<productArray.length;i++){ 
    if(masterAccessories.indexOf(productArray[i].product.id) === -1){ 
     newArray.push(productArray[i]); 
    } 
    console.log(productArray[i].product.id); 
} 

回答:jQueryを使って

for(let i=0;i<productArray.length;i++){ 
    if(masterAccessories.indexOf(productArray[i].product.id) !== -1){ 
     newArray.push(productArray[i]); 
    } 
    console.log(productArray[i].product.id); 
} 
1

var products = json;//<your product json array deserialized>; 

    var ids = [5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54]; 

    var productsContainedArray = $.grep(products, function (prod) { 
     return ids.indexOf(prod.product.id) != -1; 
    }); 
1

希望の結果に対する期待。

const 
 
    products = [ 
 
    { 
 
     productId: 1, 
 
     name: 'A', 
 
     relatedProducts: [2] 
 
    }, 
 
    { 
 
     productId: 2, 
 
     name: 'B', 
 
     relatedProducts: [1, 3] 
 
    }, 
 
    { 
 
     productId: 3, 
 
     name: 'C', 
 
     relatedProducts: [] 
 
    }, 
 
    { 
 
     productId: 4, 
 
     name: 'D', 
 
     relatedProducts: [1] 
 
    } 
 
    ]; 
 
    
 
function getReferencedProducts(input) { 
 
    let 
 
    referencedProducts = new Set(); 
 

 
    // Iterate over all the products and create a set with the IDs of all 
 
    // referenced products. Each product ID will appear only once in the set. 
 
    input.forEach(product => { 
 
    // Create a new set based on the current set and the related products of 
 
    // the current product. 
 
    referencedProducts = new Set([...referencedProducts, ...product.relatedProducts]); 
 
    }); 
 
    
 
    // Filter the products to only return those products whose ID is in 
 
    // the set of referenced products. 
 
    return input.filter(product => referencedProducts.has(product.productId)); 
 
} 
 

 
// This should output products 1, 2, 3 as product is never referenced. 
 
console.log(getReferencedProducts(products));

関連する問題