Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda