简体   繁体   中英

Extract data from Array on object in java script

I have JSON data as below: I want addition of the titleaccessed, numberoflogin for same id

    [ { id: '1651791',
        institutionname: null,
        fullname: 'Simi Abraham',
        username: ' ',
        totalrows: '46',
        numberoflogin: '1',
        logintimes: 1618963200,
        titleaccessed: '2' },
      { id: '1651791',
        institutionname: null,
        fullname: 'Simi Abraham',
        username: ' ',
        totalrows: '46',
        numberoflogin: '8',
        logintimes: 1619049600,
        titleaccessed: '18' }]
    
       The expected output is below
   
    [ { id: '1651791',
        institutionname: null,
        fullname: 'Simi Abraham',
        username: ' ',
        totalrows: '46',
        numberoflogin: '9',// addition of numberoflogin 
        logintimes: 1618963200,
        titleaccessed: '20' // addition of titleaccessed
        }]

Because the data needs to be grouped by ID, it would be possible to use find or filter to get entries with the same ID values. However, with longer lists that would get increasingly slow.

Alternatively one could use the ID as a key on an object, and then use Object.values(myObj) at the end to get the desired format:

 const data = [{id: '1651791',institutionname: null,fullname: 'Simi Abraham',username: ' ',totalrows: '46',numberoflogin: '1',logintimes: 1618963200,titleaccessed: '2'},{id: '1651791',institutionname: null,fullname: 'Simi Abraham',username: ' ',totalrows: '46',numberoflogin: '8',logintimes: 1619049600,titleaccessed: '18'}]; const out = Object.values( // format the output as required data.reduce((acc, entry) => { // reduce to a single entry const accEntry = acc[entry.id]; if (accEntry) { // if an entry exists (based on id) accEntry.numberoflogin = (parseInt(accEntry.numberoflogin) + parseInt(entry.numberoflogin)).toString(); // increment login count accEntry.titleaccessed = (parseInt(accEntry.titleaccessed) + parseInt(entry.titleaccessed)).toString(); // increment title access count } else acc[entry.id] = {...entry}; // else create entry return acc; // keep object for next iteration }, {}) // starting with an empty object ); console.log(out, data);

With large amounts of entries this will run considerably faster than find or filter methods.

Well you can do this simply with the reduce() mehtod.

Something like this

let arrayVal =[ { id: '1651791',
        institutionname: null,
        fullname: 'Simi Abraham',
        username: ' ',
        totalrows: '46',
        numberoflogin: 1,
        logintimes: 1618963200,
        titleaccessed: 2 },
      { id: '1651791',
        institutionname: null,
        fullname: 'Simi Abraham',
        username: ' ',
        totalrows: '46',
        numberoflogin: 8,
        logintimes: 1619049600,
        titleaccessed: 18 },{ id: '1651792',
        institutionname: null,
        fullname: 'Simi Abraham',
        username: ' ',
        totalrows: '46',
        numberoflogin: 8,
        logintimes: 1619049600,
        titleaccessed: 18 }]
        
    let result = arrayVal.reduce((a, c) => {
    let filtered = a.filter(el => el.id === c.id);
    if(filtered.length > 0){
        a[a.indexOf(filtered[0])].titleaccessed += +c.titleaccessed ;
        a[a.indexOf(filtered[0])].numberoflogin += +c.numberoflogin;
    }else{
        a.push(c);
    }
    return a;
}, []);

console.log(result);

I would advise you to make sure the value of numberoflogin and titleaccessed must be integer or else you will have to convert it manually using parseInt() method and then add.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM