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

152
Visualizações
How do I replace an array item with a corresponding object in a different array JavaScript

I have an object and an array. The object looks like this.

const saleProducts = {
  category: "Books",
  product_skus: [
    "14003696",
    "14003915",
    "14003699",
    "14003698",
    "14003697",
    "14003917",
  ],
};

The array has several objects inside and looks something like this:

const productDetails = [
  {
    _id: "618182229285e8d8f86be2d9b3",
    name: "JS for Dummies",
    description:
      "Learning Manual",
    category: "books",
    sku: "14003696",
    price: {
      amount: 20
    },
    images: [
      "https://uri1.png",
      "https://uri2.png",
    ],
}

As you can see, the 'sku' in the array (bookDetails) matches one of the skus in 'saleProducts'. So I would like to replace the sku with the object that has all the details. In the end, I would like the 'saleProducts' to look like this:

const saleProducts = {
  category: "Books",
  product_skus: [
    {
    _id: "618182229285e8d8f86be2d9b3",
    name: "JS for Dummies",
    description:
      "Learning Manual",
    category: "books",
    sku: "14003696",
    price: {
      amount: 20
    },
    images: [
      "https://uri1.png",
      "https://uri2.png",
    ],
]
} // ... etc. replacing each sku with the full details.

what is the simplest way to do this?

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

0

You can use a map function to replace all sku with corresponding details data:

saleProducts.product_skus = saleProducts.product_skus.map(sku => 
  productDetails.find(details => details.sku === sku) || sku
);

const saleProducts = {
  category: "Books",
  product_skus: [
    "14003696",
    "14003915",
    "14003699",
    "14003698",
    "14003697",
    "14003917",
  ],
};

const productDetails = [
  {
    _id: "618182229285e8d8f86be2d9b3",
    name: "JS for Dummies",
    description:
      "Learning Manual",
    category: "books",
    sku: "14003696",
    price: {
      amount: 20
    },
    images: [
      "https://uri1.png",
      "https://uri2.png",
    ],
  },
  {
    _id: "MOCK_1",
    name: "CSS for Dummies",
    description: "Learning Manual",
    category: "books",
    sku: "14003697",
    price: {
      amount: 10
    },
    images: [
      "https://uri3.png",
      "https://uri4.png",
    ],
  }
];

// replace sku with corresponding details data
saleProducts.product_skus = saleProducts.product_skus.map(sku => productDetails.find(details => details.sku === sku) || sku);

console.log(saleProducts);

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