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

157
Views
change object property value by other object property value Typescript

Hello I want to change a property value of an object by another property value of another object

I have two arrays:

let arr1 = [{id:1, name:'test user', imageURL:''}, {id:1, name:'test user', imageURL:''}, {id:1, name:'test user 3', imageURL:''}];
let arr2 = [{id:1, name:'test user', imageURL:'abcdefghijklmnop'}];

How can I set the value imageURL from arr2 in arr1 imageURL by id in Typescript?

pseudocode: If id in arr2 matches with id in arr1 set arr1.imageURL = arr2.imageURL

Info: I am not allowed to replace the entire object.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Assuming arr2 has only one value,

this.arr1.forEach((item) => {
  if(item.id == this.arr2[0].id) {
    item[imageUrl] = this.arr2[0].imageUrl
  }
})

If arr2 has multiple values, you can use forEach in the same too

about 4 years ago · Juan Pablo Isaza Report

0

let arr1 = [{id:1, name:'test user', imageURL:''}, {id:1, name:'test user', imageURL:''}, {id:1, name:'test user 3', imageURL:''}];
let arr2 = [{id:1, name:'test user', imageURL:'abcdefghijklmnop'}];

function setUrl(arr1, arr2, id) {
   let obj = arr2.find(prop => prop.id == id)
   if(!obj) return null;
   let response = arr1.map(prop => {
      if(prop.id === id) {
         prop.imageURL = obj.imageURL
      }
      return prop
   })
   return response
}

let result = setUrl(arr1, arr2, 1)

console.log(result)

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!