I'm trying to find the ean codes from below array in the variantCodesObject. That part I can get working however I'm struggeling to return the key from the variantCodesObject.
cart array
[
{"ean": "7350038272416","quantity": 1},
{"ean": "7350038270276","quantity": 3}
]
variantCodesObject array
[
{ 261584049: "7350038272416" },
{ 261583813: "7350038274120" },
{ 261583424: "7350038270276" },
{ 261122928: "210000018685" },
]
cart.forEach(function (cartItem){
var ean = cartItem.ean;
var qty = cartItem.quantity;
if(variantCodesObject.indexOf(ean)){
makeSomeRequest(??, qty) //How do I get the key of the found EAN's here?
}
})
In above example how do I get for ean 7350038272416 the key value 261584049?
I tried something like this:
variantCodesObject.forEach(function(item){
if(item.indexOf(ean)){
Object.keys(item).forEach(function(key) {
console.log("key:" + key + "value:" + item[key]);
});
}
});
But that returns the full variantCodesObject.
variantCodesObjectarray? It could look like this:[ { id: 261584049, ean: 7350038272416 }...]and then you could just return theid.if(variantCodesObject.indexOf(ean))makes little sense to begin with, you would be comparing an array with a scalar value here.