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.
CodePudding user response:
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)
CodePudding user response:
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; }
CodePudding user response:
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; }
CodePudding user response:
signals = [{
'signalID': '123'
},{
'signalID': '233'
},{
'signalID': '333'
},{
'signalID': '433'
},{
'signalID': '533'
},{
'signalID': '633'
},{
'signalID': '733'
}];
signalOrder = {
333:1,
433:2,
533:3
};
signals.map(obj => {
if(obj.signalID in signalOrder){
obj.orderNo = signalOrder[obj.signalID]
}
else{
obj.orderNo = null
}
})
console.log(signals)
You are right. We can use JS map function to resolve this. I have verified the below code, you can try this:
signals.map(obj => {
if(obj.signalID in signalOrder){
obj.orderNo = signalOrder[obj.signalID]
}
else{
obj.orderNo = null
}
})