2017-08-21 172 views
0

產品我有陣列列表從陣列基於IDS從另一個對象

[ 
    { 
    "id": "1", 
    "Name": "Name 1", 
    "Price": 10, 
    "inventory": 1, 
    "date_added": 1496757350 
    }, 
    { 
    "id": "2", 
    "Name": "Name 2", 
    "Price": 40, 
    "inventory": 1, 
    "date_added": 1496757290 
    }, 
... 
] 

和對象

{ 
'1':2, 
'2':15 
'10':5 
} 

這樣。因此,第一個包含產品,並且該對象包含產品的ID(鍵),以及它已添加到購物車的次數(值)。 我想實現的是基於對象基本上列出產品,在另一個對象數組中。所以在這種情況下,該列表中有3種產品。我有點迷失在這裏...減少,過濾器,地圖....我應該如何繼續? 謝謝

+3

可以顯示基於上顯示「輸入」要創建精確的輸出,? –

回答

1

可以使用reduce通過ID,使地圖上你的產品有:

{ 
    id: { /* ... product */ } 
} 

您可以使用Object.keysmap遍歷您的購物車的ID:

Object.keys(cart).map(id => productMap[id]) 

這可確保您僅在購物車中每件產品一次,而不是一次爲

另請注意,如果您向我們展示您所做的嘗試,您可能會得到更多的問題。你知道reducemap是做什麼的嗎?

const products = [ 
 
    { 
 
    "id": "1", 
 
    "Name": "Name 1", 
 
    "Price": 10, 
 
    "inventory": 1, 
 
    "date_added": 1496757350 
 
    }, 
 
    { 
 
    "id": "2", 
 
    "Name": "Name 2", 
 
    "Price": 40, 
 
    "inventory": 1, 
 
    "date_added": 1496757290 
 
    }, 
 
    { 
 
    "id": "10", 
 
    "Name": "Name 10", 
 
    "Price": 40, 
 
    "inventory": 1, 
 
    "date_added": 1496757290 
 
    } 
 
] 
 

 

 
const cart = { 
 
    "1": 2, 
 
    "2": 15, 
 
    "10": 5 
 
}; 
 

 
const productsById = products 
 
    .reduce((map, prod) => Object.assign(map, { [prod.id]: prod }), {}); 
 
    
 
const list = 
 
    Object 
 
    .keys(cart) 
 
    // Here, you can make any combination you like 
 
    // since both data are retrievable by id 
 
    
 
    // Only products: 
 
    //.map(id => productsById[id]); 
 
    
 
    // Combination 
 
    .map(id => ({ product: productsById[id], quantity: cart[id] })); 
 
     
 
console.log(list);

+0

在此之後,我如何附加每個產品的數量?在ID後的上市工作很好,剛纔我不得不附上數量... – Legycsapo

+1

在最後的'地圖',你可以從'購物車'檢索它。我將它添加到片段中。 – user3297291

+0

謝謝!這正在工作! – Legycsapo

0
let result = products.filter(function(obj) { 
    return (Object.keys(quantityObject).indexOf(obj.id) !== -1); 
}); 

這樣的事情?篩選器檢查數量對象的鍵是否存在對象ID,並根據結果返回true或false

1

如果要列出可用於其他對象(使用產品ID作爲鍵)的產品,則需要filter

const products = [ 
 
    { 
 
    "id": "1", 
 
    "Name": "Name 1", 
 
    "Price": 10, 
 
    "inventory": 1, 
 
    "date_added": 1496757350 
 
    }, 
 
    { 
 
    "id": "2", 
 
    "Name": "Name 2", 
 
    "Price": 40, 
 
    "inventory": 1, 
 
    "date_added": 1496757290 
 
    }, 
 

 
] 
 

 
const ids = { 
 
'1': 2 
 
} 
 

 
console.log(products.filter(product => product.id in ids))

1

你可以篩選產品,並建立一個新的結果與車的量設定。

var products = [{ id: "1", Name: "Name 1", Price: 10, inventory: 1, date_added: 1496757350 }, { id: "2", Name: "Name 2", Price: 40, inventory: 1, date_added: 1496757290 }, { id: "4", Name: "Name 4", Price: 30, inventory: 1, date_added: 1496757290 }], 
 
    cart = { 1: 2, 2: 15, 10: 5 }, 
 
    selectedProducts = products.filter(function (o) { return o.id in cart; }); 
 

 

 
console.log(selectedProducts.map(function (o) { 
 
    return ['id', 'Name'].map(function (k) { 
 
     return k + ': ' + o[k]; 
 
    }).concat('count: ' + cart[o.id]).join('; '); 
 
}));

1

您可以創建一個輸出對象,並用它已被添加的次數增加的項目。

var items = [{ 
 
    "id": "1", 
 
    "Name": "Name 1", 
 
    "Price": 10, 
 
    "inventory": 1, 
 
    "date_added": 1496757350 
 
}, { 
 
    "id": "2", 
 
    "Name": "Name 2", 
 
    "Price": 40, 
 
    "inventory": 1, 
 
    "date_added": 1496757290 
 
}]; 
 

 
var cart = { 
 
    '1': 2, 
 
    '2': 15, 
 
    '10': 5 
 
} 
 

 
var output = {}; 
 

 
for (id in cart) { 
 
    item = items.find(x => x.id === id); 
 
    if (item != null) 
 
    output[id] = { 
 
     item: item, 
 
     count: cart[id] 
 
    }; 
 
} 
 
console.log(output);

1

對於速度的原因,查找的等成陣,我傾向於創建使用其他關聯數組是一種索引。像下面的東西..

var products = [ 
 
    { 
 
    "id": "1", 
 
    "Name": "Name 1", 
 
    "Price": 10, 
 
    "inventory": 1, 
 
    "date_added": 1496757350 
 
    }, 
 
    { 
 
    "id": "2", 
 
    "Name": "Name 2", 
 
    "Price": 40, 
 
    "inventory": 1, 
 
    "date_added": 1496757290 
 
    } 
 
]; 
 

 
var basket = 
 
{ 
 
    '1':2, 
 
    '2':15 
 
}; 
 

 
//first lets create a kind of index to products.. 
 
var ixProducts = products.reduce((a, v) => { a[v.id] = v; return a; }, {}); 
 

 
Object.keys(basket).forEach((k) => { 
 
    console.log({ 
 
    qty: basket[k], 
 
    product: ixProducts[k] 
 
    }); 
 
});