I have an array of object in which I want to manipulate.
This is the array
const customerInformation = [
{"id":"12345678","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"1942f0e6","ownerId":"234566654","exec":{"execId":"20326379004","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"John Smith","accountId":"234566654","comid":"cs169612397275616092-1","status":"Active","phoneNumber":"+442222222222","extensionId":"234566654","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"6374595864","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"1942f0e6","ownerId":"155464348743","exec":{"execId":"1521648743","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Math Lo","accountId":"26726447342","comid":"cs169612397275616092-1","status":"Active","phoneNumber":"+442222222222","extensionId":"234566654","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"98736478","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"hblwrv890","ownerId":"98765322","exec":{"execId":"20326379004","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"James Olive","accountId":"234566654","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"256644477","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"sbdbbgbg","ownerId":"32545453565","exec":{"execId":"32254655464","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Ben Right","accountId":"234566654","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
{"id":"99326672378372","timestamp":"2019-01-21T11:53:29.338Z","userConnection":"sjvjvnfrrev","ownerId":"28643872329","exec":{"execId":"268474374938","eventTime":"2019-01-21T11:53:29.223Z","communication":[{"name":"Lowe John","accountId":"2225454354","comid":"cs169612397275616092-2","status":"Active","phoneNumber":"+442222222222","extensionId":"2763648749","missedCall":false,"standAlone":false,"muted":false}]}},
]
What I would like to do is manipulate the array to produce a much smaller array. the array should look like this. Please note it's using a unique comid
, that's why you only get two results. Also the second value has to return undefined, as it's the requirement
const obj = [{
'id': '12345678',
'ownerId': '234566654'
'userConnection': '1942f0e6',
'status' : 'Active',
'comid' : 'cs169612397275616092-1',
'extensionId' : '234566654',
'phoneNumber' : '+442222222222',
'startAt' : '2019-01-21T11:53:29.223Z',
},{
'id': undefined,
'ownerId': '98765322'
'userConnection': 'hblwrv890',
'status' : 'Active',
'comid' : 'cs169612397275616092-2',
'extensionId' : '2763648749',
'phoneNumber' : '+442222222222',
'startAt' : '2019-01-21T11:53:29.223Z',
}];
This is what my code looks like at the moment
var newArray= [];
customerInformation.forEach(function(element){
newArray.push(element.exec.communication[0].comid);
})
const uniqueId = [...new Set(newArray)];
var result = uniqueId.map(function(el) {
const [key, user] = Object.entries(customerInformation).find(([key, user]) => user.exec.communication[0].comid === el);
var o = Object.assign({});
o.extensionId = user.id
o.ownerId= user.exec.communication[0].comid
return o
})
This is as far as I could get. Please i just want to understand what i am doing wrong. Presume there is a better way to do this. Please it sounds lazy but am looking for a better solution