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

188
Views
Overwrite value of objects in Array by property value

I have two arrays of objects. How can i take the property from one array and override the 'value' property of the other array? Currently I'm doing this but it seems quite wonky to do it this way. The original array 'streamingTiles' contains other properties, which is why i have the '...'

Goal is to replace 'value' in streamingTiles with 'counts' value from refTiles based on the matching UID per object

const streamingTiles = [
    {uid: 57, value: 10, ...},
    {uid: 30, value: 51, ...},
    {uid: 14, value: 24, ...},
    {uid: 47, value: 73, ...},
]

const refTiles = [
    {uid: 14, counts: 10},
    {uid: 57, counts: 51},
    {uid: 30, counts: 24},
    {uid: 47, counts: 73},
]

// replace value in streamingTiles with 'count' value from refTiles
const tiles = streamingTiles.map((item) => {
  var refObj = refTiles.find((obj) => {
    return obj.uid === item.uid;
  });

  return { ...item, ...{ value: refObj["counts"] } };
});

UPDATED QUESTION

If I renamed 'counts' to be 'value', would the same solution of replacing the 'value' in streamingTiles array with the 'value' from refTiles based on matching UID apply?

const streamingTiles = [
    {uid: 57, value: 10, ...},
    {uid: 30, value: 51, ...},
    {uid: 14, value: 24, ...},
    {uid: 47, value: 73, ...},
]

const refTiles = [
    {uid: 14, value: 10},
    {uid: 57, value: 51},
    {uid: 30, value: 24},
    {uid: 47, value: 73},
]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Process the refTiles into an object to avoid find calls for each item.

const streamingTiles = [
  { uid: 57, value: 10 },
  { uid: 30, value: 51 },
  { uid: 14, value: 24 },
  { uid: 47, value: 73 },
];

const refTiles = [
  { uid: 14, counts: 10 },
  { uid: 57, counts: 51 },
  { uid: 30, counts: 24 },
  { uid: 47, counts: 73 },
];

const refs = refTiles.reduce(
  (acc, curr) => ((acc[curr.uid] = curr.counts), acc),
  {}
);

const titles = streamingTiles.map((item) => ({
  ...item,
  value: refs[item.uid] ?? item.value,
}));

console.log(titles);

about 4 years ago · Juan Pablo Isaza Report

0

You can just remove the unnecessary object destruction:

const tiles = streamingTiles.map((item) => {
  var refObj = refTiles.find((obj) => {
    return obj.uid === item.uid;
  });

  return { ...item, value: refObj["counts"]};
});
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!