Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

86
Visualizações
Javascript in array then return key

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.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can do this by checking the value of the object in the variantCodesObject against the .ean property on each cart item. If it matches, do whatever you'd like with the key

cart = [
  {"ean": "7350038272416","quantity": 1},
  {"ean": "7350038270276","quantity": 3}
]


variantCodesObject = [
 { 261584049: "7350038272416" },
 { 261583813: "7350038274120" },
 { 261583424: "7350038270276" },
 { 261122928: "210000018685" },
]


cart.forEach(item => {
  variantCodesObject.forEach(obj => {
    Object.entries(obj).forEach(([key, value]) => {
      if (value === item.ean) {
        console.log(key);
      }
    });
  })
})

about 4 years ago · Juan Pablo Isaza Relatório

0

One method would be to use Array.reduce() to gather data from the array:

const cart =
[
  {"ean": "7350038272416","quantity": 1},
  {"ean": "7350038270276","quantity": 3}
]


const variantCodesObject =
[
 { 261584049: "7350038272416" },
 { 261583813: "7350038274120" },
 { 261583424: "7350038270276" },
 { 83424: "7350038270276" },
 { 261122928: "210000018685" },
]

cart.forEach(function (cartItem){    
  var ean = cartItem.ean;
  var qty = cartItem.quantity;

  const keys = variantCodesObject.reduce((ret, obj) =>
  {
    for(let key in obj)
      if (obj[key] == ean)
        ret[ret.length] = key;

    return ret;
  }, []);

  console.log(ean, "=", keys);

})

about 4 years ago · Juan Pablo Isaza Relatório

0

You can create a "Reverse Lookup Table". Here is how you can do that.

var cartArray = [
  { ean: '7350038272416', quantity: 1 },
  { ean: '7350038270276', quantity: 3 },
];

var variantCodesObjectArray = [
  { 261584049: '7350038272416' },
  { 261583813: '7350038274120' },
  { 261583424: '7350038270276' },
  { 261122928: '210000018685' },
];

// We are creating a lookup / dictionary by mapping the "EAN" to the "CODE"
var reverMappedCodesObjectArray = {};

variantCodesObjectArray.forEach(function (entry) {
  var key = Object.keys(entry)[0];
  reverMappedCodesObjectArray[entry[key]] = key;
});

cartArray.forEach(function (cartItem) {
  var ean = cartItem.ean;
  var qty = cartItem.quantity;

  console.log(reverMappedCodesObjectArray[ean]);
});

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda