Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!