Home > Back-end >  How to iterate through an array of objects and sum a specific property with Javascript
How to iterate through an array of objects and sum a specific property with Javascript

Time:09-22

I have an array of objects and I need to scroll through them and add Maria and Cristiano's battlesWon, in the end I need to display how many BattleWon each one won as shown in the example

 const array = [
      {
        id: '147acaa3-363c-4a28-af43-fcc035a1d500',
        arena: 'Philippine Arena',
        firstParticipant: {
          address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661',
          name: 'Cristiano',
          battlesWon: 0
        },
        secondParticipant: {
          address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644',
          name: 'Maria',
          battlesWon: 1
        },
        logs: [ [Object] ]
      },
      {
        id: 'b2ef2d28-d84d-4cc9-946f-3d57b8ce05ab',
        arena: 'Greensboro Coliseum',
        firstParticipant: {
          address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644',
          name: 'Maria',
          battlesWon: 1
        },
        secondParticipant: {
          address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661',
          name: 'Cristiano',
          battlesWon: 0
        },
        logs: [ [Object] ]
      }
    ]

Expected:

{
  Cristiano: 0,
  Maria: 2
}

CodePudding user response:

I think one possible solution is to loop over the array using forEach and then adding the values to a results dictionary, as you can see here:

let list = [
    {
        id: '147acaa3-363c-4a28-af43-fcc035a1d500',
        arena: 'Philippine Arena',
        firstParticipant: {
            address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661',
            name: 'Cristiano',
            battlesWon: 0
        },
        secondParticipant: {
            address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644',
            name: 'Maria',
            battlesWon: 1
        },
        logs: [[Object]]
    },
    {
        id: 'b2ef2d28-d84d-4cc9-946f-3d57b8ce05ab',
        arena: 'Greensboro Coliseum',
        firstParticipant: {
            address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644',
            name: 'Maria',
            battlesWon: 1
        },
        secondParticipant: {
            address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661',
            name: 'Cristiano',
            battlesWon: 0
        },
        logs: [[Object]]
    }
]

let results = {
    Cristiano: 0,
    Maria: 0
}

list.forEach(arena => {
    results[arena.firstParticipant.name]  = arena.firstParticipant.battlesWon
    results[arena.secondParticipant.name]  = arena.secondParticipant.battlesWon
})

console.log(results)

CodePudding user response:

Please use this code.

const arr = [
  {
    id: '147acaa3-363c-4a28-af43-fcc035a1d500',
    arena: 'Philippine Arena',
    firstParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661',
      name: 'Cristiano',
      battlesWon: 0
    },
    secondParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644',
      name: 'Maria',
      battlesWon: 1
    },
    logs: [ [Object] ]
  },
  {
    id: 'b2ef2d28-d84d-4cc9-946f-3d57b8ce05ab',
    arena: 'Greensboro Coliseum',
    firstParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644',
      name: 'Maria',
      battlesWon: 1
    },
    secondParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661',
      name: 'Cristiano',
      battlesWon: 0
    },
    logs: [ [Object] ]
  }
]

let result = {};

arr.forEach(val => {
  ['firstParticipant', 'secondParticipant'].forEach(key => {
    if(result.hasOwnProperty(val[key]['name'])) {
      result[val[key]['name']]  = val[key]['battlesWon'];
    } else {
      result[val[key]['name']] = val[key]['battlesWon'];
    }
  })
})

console.log(result);

CodePudding user response:

You can do this:

const json = [
  {
    id: '147acaa3-363c-4a28-af43-fcc035a1d500',
    arena: 'Philippine Arena',
    firstParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661',
      name: 'Cristiano',
      battlesWon: 0
    },
    secondParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644',
      name: 'Maria',
      battlesWon: 1
    },
    logs: [ [Object] ]
  },
  {
    id: 'b2ef2d28-d84d-4cc9-946f-3d57b8ce05ab',
    arena: 'Greensboro Coliseum',
    firstParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644',
      name: 'Maria',
      battlesWon: 1
    },
    secondParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661',
      name: 'Cristiano',
      battlesWon: 0
    },
    logs: [ [Object] ]
  }
]

var newJson = new Array();
json.map((battle) => {
    newJson[battle.firstParticipant.name] = newJson[battle.firstParticipant.name] != null ? battle.firstParticipant.battlesWon   newJson[battle.firstParticipant.name] :  battle.firstParticipant.battlesWon;
    newJson[battle.secondParticipant.name] = newJson[battle.secondParticipant.name] != null ? battle.secondParticipant.battlesWon   newJson[battle.secondParticipant.name] :  battle.secondParticipant.battlesWon;
});

CodePudding user response:

You can use Array.prototype.reduce to go through each entry in your data, and then write participant data into the accumulator. The advantage is that you don't need to create a separate object outside your function.

Then, in each iteration of the reduce(), you simply use a generic function that writes participant data into the accumulator:

function handleParticipant(participant, acc) {
  const { name, battlesWon } = participant;
  acc[name] = (acc[name] || 0)   battlesWon;
}

The real logic is in the line acc[name] = (acc[name] || 0) battlesWon, that basically says: if name is found in the dictionary, then add battlesWon to its value, otherwise you add battlesWon to a starting value of 0.

See proof-of-concept below:

const data = [
  {
    id: '147acaa3-363c-4a28-af43-fcc035a1d500',
    arena: 'Philippine Arena',
    firstParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661',
      name: 'Cristiano',
      battlesWon: 0
    },
    secondParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644',
      name: 'Maria',
      battlesWon: 1
    }
  },
  {
    id: 'b2ef2d28-d84d-4cc9-946f-3d57b8ce05ab',
    arena: 'Greensboro Coliseum',
    firstParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644',
      name: 'Maria',
      battlesWon: 1
    },
    secondParticipant: {
      address: '0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661',
      name: 'Cristiano',
      battlesWon: 0
    }
  }
];

function handleParticipant(participant, acc) {
  const { name, battlesWon } = participant;
  acc[name] = (acc[name] || 0)   battlesWon;
}

const battlesWonByName = data.reduce((acc, cur) => {
  const { firstParticipant, secondParticipant } = cur;
  
  handleParticipant(firstParticipant, acc);
  handleParticipant(secondParticipant, acc);
  
  return acc;
}, {});

console.log(battlesWonByName);

CodePudding user response:

You can easily achieve the result using reduce

arr.reduce((acc, curr) => {
  const { firstParticipant: { name: a, battlesWon: m }, secondParticipant: { name: b, battlesWon: n } } = curr;
  acc[a] = (acc[a] ?? 0)   m
  acc[b] = (acc[b] ?? 0)   n
  return acc;
}, {});

1)

const arr = [
  {
    id: "147acaa3-363c-4a28-af43-fcc035a1d500",
    arena: "Philippine Arena",
    firstParticipant: {
      address: "0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661",
      name: "Cristiano",
      battlesWon: 0,
    },
    secondParticipant: {
      address: "0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644",
      name: "Maria",
      battlesWon: 1,
    },
    logs: [[Object]],
  },
  {
    id: "b2ef2d28-d84d-4cc9-946f-3d57b8ce05ab",
    arena: "Greensboro Coliseum",
    firstParticipant: {
      address: "0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644",
      name: "Maria",
      battlesWon: 1,
    },
    secondParticipant: {
      address: "0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661",
      name: "Cristiano",
      battlesWon: 0,
    },
    logs: [[Object]],
  },
];

const result = arr.reduce((acc, curr) => {
  const {
    firstParticipant: { name: name1, battlesWon: battlesWon1 },
    secondParticipant: { name: name2, battlesWon: battlesWon2 },
  } = curr;

  acc[name1] ? (acc[name1]  = battlesWon1) : (acc[name1] = battlesWon1);
  acc[name2] ? (acc[name2]  = battlesWon2) : (acc[name2] = battlesWon2);

  return acc;
}, {});

console.log(result);

2)

const arr = [
  {
    id: "147acaa3-363c-4a28-af43-fcc035a1d500",
    arena: "Philippine Arena",
    firstParticipant: {
      address: "0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661",
      name: "Cristiano",
      battlesWon: 0,
    },
    secondParticipant: {
      address: "0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644",
      name: "Maria",
      battlesWon: 1,
    },
    logs: [[Object]],
  },
  {
    id: "b2ef2d28-d84d-4cc9-946f-3d57b8ce05ab",
    arena: "Greensboro Coliseum",
    firstParticipant: {
      address: "0x3ba59bcc1a02cb46e7de35fb0bacc860bf075644",
      name: "Maria",
      battlesWon: 1,
    },
    secondParticipant: {
      address: "0x3ba59bcc1a02cb46e7de35fb0bacc860bf075661",
      name: "Cristiano",
      battlesWon: 0,
    },
    logs: [[Object]],
  },
];

const result = arr.reduce((acc, { firstParticipant, secondParticipant }) => {
  acc[firstParticipant.name]
    ? (acc[firstParticipant.name]  = firstParticipant.battlesWon)
    : (acc[firstParticipant.name] = firstParticipant.battlesWon);

  acc[secondParticipant.name]
    ? (acc[secondParticipant.name]  = secondParticipant.battlesWon)
    : (acc[secondParticipant.name] = secondParticipant.battlesWon);
  return acc;
}, {});

console.log(result);

  • Related