-2

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

dannywonda
  • 29
  • 6
  • 2
    The `events` variable looks never to be defined. Can you explain in words how the array is supposed to be combined? Eg why is the first id `12345678` but the second is `undefined` (and same for the rest of the properties, other than `comid`)? – CertainPerformance Feb 15 '20 at 01:09
  • 1
    This is not a multidimensional array, this is just an array of objects with a key that has another array in it. A multidimensional array looks like this: `var a = [ ['foo', 1], ['bar', 2] ];` – Afro Feb 15 '20 at 01:14
  • Yes your right @afro its array of object. And i have corrected the code – dannywonda Feb 15 '20 at 01:16

3 Answers3

1

Try the reduce and concat methods on your 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}]}},

]

const obj = customerInformation.reduce(function( result, item ){
    return result.concat({
        id: item.id,
        ownerId: item.ownerId,
        userConnection: item.userConnection
        });

}, []);
console.info(obj);
Shawn W
  • 566
  • 4
  • 13
  • This does not result in the output OP says is desired – CertainPerformance Feb 15 '20 at 05:07
  • The output is the same, I simply didn't use the full list of desired properties, is that what you mean? – Shawn W Feb 15 '20 at 13:17
  • OP's desired output is an array with 2 items. You're creating an array with an item for every item in the original array. (For that, easier to use `.map` than `.reduce`, but it's still not what OP is looking for - and the logic OP is looking for is unclear to me, other than that the result should have 2 items) – CertainPerformance Feb 15 '20 at 13:21
0

You did not explain how to combine the elements, so here is a wild guess below. I won't try to decode what you're doing wrong, because the code you posted does not do what you describe? (e.g. You assign comid to o.ownerId, but your expected result does not show that, and you assign user.id to o.extensionId, but I do not see any id property in the original Array with the same value as an extensionId in the expected result)

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}]}}
];

const result = customerInformation.reduce((res, el) => {
  const com = el.exec.communication[0];
  // If the result array does not contain that comid yet
  if (!res.some(el2 => el2.comid === com.comid)) {
    // Add it
    res.push({
      id: el.id,
      ownerId: el.ownerId,
      userConnection: el.userConnection,
      status: com.status,
      comid: com.comid,
      extensionId: com.extensionId,
      phoneNumber: com.phoneNumber,
      startAt: el.exec.eventTime
    });
  }
  return res;
}, []);

// Because "it's the requirement"
result[1].id = undefined;

console.log(result);
blex
  • 24,941
  • 5
  • 39
  • 72
-1

Your example is a bit confusing, but I'll take a stab at helping.

What it sounds like you want to do is dedupe the original array, based on a key in one of the nested objects. Common way to dedupe like this is to use a map object. and you also want to collect info from that object in a new array. You can actually do both these operations at the same time because of how object references work.

Full disclosuer: This could be written nicer with some newer JS syntax, tried to keep it simple :)

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}]}},
]

const map = {};
const newArr = [];

for (const info of customerInformation) {
    const id = info.exec.communication[0].comid;
    if (!map[id]) {
 map[id] = {};
 newArr.push(map[id]);
    }
    Object.assign(map[id], {
 ownerId: info.ownerId,
 comid: id,
 extensionId: info.id,
 userConnection: info.userConnection
 // add whatever fields and other logic you want here
    });
}

console.log('customerInformation', newArr);

I didn't understand some of the question so just trying to do my best to lend a hand. Wasn't sure what you meant by Also the second value has to return undefined, as it's the requirement

dotDeeka
  • 54
  • 5