-2

I have some code where I am looping through some records in a table, and whilst doing this I'm organising this data into a JavaScript object array.

For example the data below.

enter image description here

I need to ensure that if a record "Name" has already been recorded, then rather than create a new item it uses the existing item so that the end result will be an object or array like this:

{
    "bobjones": {
         "sessions": 7
    },
    "annfrank": {
         "sessions": 4
    },
    "fredsmith": {
         "sessions": 4
    }
}

Doing this in PHP is easy, but I'm struggling with this in JS due to the way keys are interpreted. Could somebody provide some direction?

isherwood
  • 58,414
  • 16
  • 114
  • 157
Adam Moss
  • 5,582
  • 13
  • 46
  • 64
  • 1
    Could you provide input data? – Nenad Vracar Jan 10 '19 at 15:48
  • An array would call for some brackets, where you have all braces. Was that a mistake in your example or is that a single array element? – isherwood Jan 10 '19 at 15:49
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Liam Jan 10 '19 at 15:49
  • Your object seems malformed. It's make much more sense to have something more like `[{name:'bobjones', sessions:'7'},...]`. *I'm organising this data into a JavaScript object array* I see an object, I don't see an array – Liam Jan 10 '19 at 15:50

3 Answers3

2
const inputData = [
  { name: 'Bob Jones', date: '----', session: 1 },
  { name: 'Ann Frank', date: '----', session: 1 },
  { name: 'Bob Jones', date: '----', session: 1 },
  { name: 'Fred Smith', date: '----', session: 1 },
]

var outputData = {}
for(let doc of inputData) {
  if(outputData[doc.name]) {
    outputData[doc.name].session = outputData[doc.name].session + doc.session
  } else {
    outputData[doc.name] = doc
  }
}

console.log(outputData)

Now outputData is:

{ 'Bob Jones': { name: 'Bob Jones', date: '----', session: 2 },
  'Ann Frank': { name: 'Ann Frank', date: '----', session: 1 },
  'Fred Smith': { name: 'Fred Smith', date: '----', session: 1 } }
Fathan
  • 112
  • 1
  • 4
0

You can just test to see if the object property exists. If so, add to its sessions property, if not, set it.

if(obj[prop]) {
    obj[prop].sessions += sessions;
} else {
    obj[prop] = { sessions };
}

In the first if statement, if you're concerned that obj[prop] may exist but obj[prop].sessions may not, you can change it to if(obj[prop] && obj[prop].sessions) { ... }

Nick
  • 16,066
  • 3
  • 16
  • 32
0

You could use the reduce method, assuming that you input data is an array of objects (since, that is generally how you get data when consulting a database).

const table = [
    {name: "Bob Jones", date: "12-12-2019", sessions: 3},
    {name: "Ann Frank", date: "12-9-2019", sessions: 4},
    {name: "Bob Jones", date: "12-9-2019", sessions: 2},
    {name: "Fred Smith", date: "12-9-2019", sessions: 4}
];

let res = table.reduce((res, {name, date, sessions}) =>
{
    if (res[[name]])
        res[[name]].sessions += sessions;
    else
        res[[name]] = {sessions: sessions};

    return res;
}, {});

console.log(res);
Shidersz
  • 16,846
  • 2
  • 23
  • 48