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

116
Views
how to add a property in object inside array of object if it matches the id

I am newbie in angular, facing one issue if somebody helps then it will be appreciable I have a array of objects like below

signals = [{
'signalID': '123'
},{
'signalID': '233'
},{
'signalID': '333'
},{
'signalID': '433'
},{
'signalID': '533'
},{
'signalID': '633'
},{
'signalID': '733'
}]

I have an object like below

signalOrder = {
333:1,
433:2,
533:3
}

i want to add an property to signals array as orderNo if signalId is matched in signalOrder object else orderNo should be null. Result expected :

signals = [{
'signalID': '123',
'orderNo':null
},{
'signalID': '233',
'orderNo':null
},{
'signalID': '333',
'orderNo':1
},{
'signalID': '433',
'orderNo':2
},{
'signalID': '533',
'orderNo':3
},{
'signalID': '633',
'orderNo':null
},{
'signalID': '733',
'orderNo':null
}]

I hope it should work with javascript map function but don't know how to use it.

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

0

You can do it like :

let signals = [{'signalID': '123'},{'signalID': '233'},{'signalID': '333'},{'signalID': '433'},{'signalID': '533'},{'signalID': '633'},{'signalID': '733'}];

let signalOrder = {333:1,433:2,533:3}

let mapped = signals.map(x=>{
  return {
    signalID:x.signalID,
    orderNo:signalOrder[x.signalID]
  }
})

console.log(mapped)

about 4 years ago · Juan Pablo Isaza Report

0

You can use Array.map() to create the result you wish. For each signal item, we'll look up the orderNo in the signalOrder object. If not present, we'll set it to null.

const signals = [{ 'signalID': '123' },{ 'signalID': '233' },{ 'signalID': '333' },{ 'signalID': '433' },{ 'signalID': '533' },{ 'signalID': '633' },{ 'signalID': '733' }];

const signalOrder = {
  333:1,
  433:2,
  533:3
}

const result = signals.map(({signalID}) => ({ 
    signalID,
    orderNo: signalOrder[signalID] || null
}))

console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

This has nothing to do with Angular or Typescript. It could be solved with vanilla JS Array methods.

Here is a method using Array#map, spread syntax, falsy test using OR ||. Attempting signalOrder[signal.signalID] would return undefined for unavailable properties, which is falsy. In these cases, null would be used.

Try the following

const signals = [{ 'signalID': '123' },{ 'signalID': '233' },{ 'signalID': '333' },{ 'signalID': '433' },{ 'signalID': '533' },{ 'signalID': '633' },{ 'signalID': '733' }]
const signalOrder = { 333:1, 433:2, 533:3 }

const result = signals.map(signal => ({
  ...signal,
  orderNo: signalOrder[signal.signalID] || null
}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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!