在java脚本中从对象上的数组提取数据

tktrz96b  于 2021-09-13  发布在  Java
关注(0)|答案(2)|浏览(202)

我有如下的json数据:我想为相同的id添加titleaccessed和numberoflogin

[ { 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
        }]
sdnqo3pr

sdnqo3pr1#

您可以简单地使用reduce()方法来实现这一点。
像这样的

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);

我建议您确保numberoflogin和titleaccessed的值必须是整数,否则您必须使用parseint()方法手动转换它,然后添加。

mwg9r5ms

mwg9r5ms2#

因为数据需要按id分组,所以可以使用 findfilter 获取具有相同id值的条目。然而,随着更长的列表,这将变得越来越慢。
或者,可以将id用作对象上的键,然后使用 Object.values(myObj) 最后,要获得所需的格式,请执行以下操作:

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);

对于大量条目,这将比 findfilter 方法。

相关问题