Home > Software engineering >  How to add the property of same occurrence of object in an array?
How to add the property of same occurrence of object in an array?

Time:10-04

Hey everyone I have this data coming from an API source what you can see here you will find some duplicate for each of the object what I want is, I want to add the stars & destructionPercentage with the once which matches with its duplicate. Since the property name can be changed anytime. I want to do the match with the property tag since its unique and will never change, so what will happen here is as you can see a clan name of Air Hounds 3 you will find another object with Air Hounds 3 down below. so the first AirHounds 3 has a star 9 and a destructionPercenatge of 23.46 now there is another Air Hounds 3 with a value of stars 39 and a destructionPercentage of 76.6 so what I want is I want to add this stars & destructionPercentage of 2 objects and want to return the sum of it as 1 object in a new array with all other properties it has in the object, this rule will be applied to all the other 7 datas with their matching their duplicated once. Note currently there is 1 occurrence of duplicate but there will more in future I want the same to work for them as well. So there is currently 16 objects so it should return 8. If there is more than 16 objects like 24 or 32 or even more it should no matter what it is it should return 8 Can anyone tell me how can I do it. Thanks in Advance.

here is the data:

[
  {
    state: 'inWar',
    name: '一群爷们在战斗',
    tag: '#PQYGYLPJ',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png',
      large: 'https://api-assets.clashofclans.com/badges/512/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png'
    },
    stars: 2,
    destructionPercentage: 4.933333333333334
  },
  {
    state: 'inWar',
    name: '雨过天晴',
    tag: '#R8JRUJCU',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png',
      large: 'https://api-assets.clashofclans.com/badges/512/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png'
    },
    stars: 12,
    destructionPercentage: 35.13333333333333
  },
  {
    state: 'inWar',
    name: '宝贝轻点',
    tag: '#2PU8GLGGL',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png',
      large: 'https://api-assets.clashofclans.com/badges/512/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png'
    },
    stars: 3,
    destructionPercentage: 5.333333333333333
  },
  {
    state: 'inWar',
    name: 'ubw',
    tag: '#9LY9YLQV',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png',
      large: 'https://api-assets.clashofclans.com/badges/512/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png'
    },
    stars: 36,
    destructionPercentage: 80
  },
  {
    state: 'inWar',
    name: 'Air Hounds 3',
    tag: '#2P9UG82L',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png',
      large: 'https://api-assets.clashofclans.com/badges/512/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png'
    },
    stars: 15,
    destructionPercentage: 39.93333333333333
  },
  {
    state: 'inWar',
    name: '青春无敌',
    tag: '#9LLVCP9V',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png',
      large: 'https://api-assets.clashofclans.com/badges/512/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png'
    },
    stars: 4,
    destructionPercentage: 7.933333333333334
  },
  {
    state: 'inWar',
    name: 'blue vapor',
    tag: '#GJ98C29G',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png',
      large: 'https://api-assets.clashofclans.com/badges/512/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png'
    },
    stars: 30,
    destructionPercentage: 73.2
  },
  {
    state: 'inWar',
    name: 'Trip to Walhall',
    tag: '#QPYC0QYJ',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png',
      large: 'https://api-assets.clashofclans.com/badges/512/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png'
    },
    stars: 30,
    destructionPercentage: 66.53333333333333
  },
  {
    state: 'warEnded',
    name: '一群爷们在战斗',
    tag: '#PQYGYLPJ',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png',
      large: 'https://api-assets.clashofclans.com/badges/512/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png'
    },
    stars: 43,
    destructionPercentage: 82.8
  },
  {
    state: 'warEnded',
    name: 'ubw',
    tag: '#9LY9YLQV',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png',
      large: 'https://api-assets.clashofclans.com/badges/512/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png'
    },
    stars: 33,
    destructionPercentage: 78.66666666666667
  },
  {
    state: 'warEnded',
    name: '宝贝轻点',
    tag: '#2PU8GLGGL',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png',
      large: 'https://api-assets.clashofclans.com/badges/512/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png'
    },
    stars: 8,
    destructionPercentage: 17.8
  },
  {
    state: 'warEnded',
    name: 'Trip to Walhall',
    tag: '#QPYC0QYJ',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png',
      large: 'https://api-assets.clashofclans.com/badges/512/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png'
    },
    stars: 55,
    destructionPercentage: 100
  },
  {
    state: 'warEnded',
    name: 'blue vapor',
    tag: '#GJ98C29G',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png',
      large: 'https://api-assets.clashofclans.com/badges/512/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png'
    },
    stars: 28,
    destructionPercentage: 68.73333333333333
  },
  {
    state: 'warEnded',
    name: 'Air Hounds 3',
    tag: '#2P9UG82L',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png',
      large: 'https://api-assets.clashofclans.com/badges/512/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png'
    },
    stars: 39,
    destructionPercentage: 78.33333333333333
  },
  {
    state: 'warEnded',
    name: '雨过天晴',
    tag: '#R8JRUJCU',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png',
      large: 'https://api-assets.clashofclans.com/badges/512/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png'
    },
    stars: 32,
    destructionPercentage: 76.6
  },
  {
    state: 'warEnded',
    name: '青春无敌',
    tag: '#9LLVCP9V',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png',
      large: 'https://api-assets.clashofclans.com/badges/512/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png'
    },
    stars: 44,
    destructionPercentage: 80.6
  }
]

CodePudding user response:

You can achieve this by using Array.prototype.forEach() on your players list. Then merge 2 objects with same tag to one :

const list = [
  {
    state: 'inWar',
    name: '一群爷们在战斗',
    tag: '#PQYGYLPJ',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png',
      large: 'https://api-assets.clashofclans.com/badges/512/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png'
    },
    stars: 2,
    destructionPercentage: 4.933333333333334
  },
  {
    state: 'inWar',
    name: '雨过天晴',
    tag: '#R8JRUJCU',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png',
      large: 'https://api-assets.clashofclans.com/badges/512/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png'
    },
    stars: 12,
    destructionPercentage: 35.13333333333333
  },
  {
    state: 'inWar',
    name: '宝贝轻点',
    tag: '#2PU8GLGGL',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png',
      large: 'https://api-assets.clashofclans.com/badges/512/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png'
    },
    stars: 3,
    destructionPercentage: 5.333333333333333
  },
  {
    state: 'inWar',
    name: 'ubw',
    tag: '#9LY9YLQV',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png',
      large: 'https://api-assets.clashofclans.com/badges/512/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png'
    },
    stars: 36,
    destructionPercentage: 80
  },
  {
    state: 'inWar',
    name: 'Air Hounds 3',
    tag: '#2P9UG82L',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png',
      large: 'https://api-assets.clashofclans.com/badges/512/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png'
    },
    stars: 15,
    destructionPercentage: 39.93333333333333
  },
  {
    state: 'inWar',
    name: '青春无敌',
    tag: '#9LLVCP9V',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png',
      large: 'https://api-assets.clashofclans.com/badges/512/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png'
    },
    stars: 4,
    destructionPercentage: 7.933333333333334
  },
  {
    state: 'inWar',
    name: 'blue vapor',
    tag: '#GJ98C29G',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png',
      large: 'https://api-assets.clashofclans.com/badges/512/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png'
    },
    stars: 30,
    destructionPercentage: 73.2
  },
  {
    state: 'inWar',
    name: 'Trip to Walhall',
    tag: '#QPYC0QYJ',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png',
      large: 'https://api-assets.clashofclans.com/badges/512/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png'
    },
    stars: 30,
    destructionPercentage: 66.53333333333333
  },
  {
    state: 'warEnded',
    name: '一群爷们在战斗',
    tag: '#PQYGYLPJ',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png',
      large: 'https://api-assets.clashofclans.com/badges/512/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/n6glPC-kovZwdM9gNds-44_DOtLB1W_qTX3DatIxUhc.png'
    },
    stars: 43,
    destructionPercentage: 82.8
  },
  {
    state: 'warEnded',
    name: 'ubw',
    tag: '#9LY9YLQV',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png',
      large: 'https://api-assets.clashofclans.com/badges/512/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/sfoosjVYjGio8qL-ZrpLun6_jGI5pEFj40VJiL5c9YQ.png'
    },
    stars: 33,
    destructionPercentage: 78.66666666666667
  },
  {
    state: 'warEnded',
    name: '宝贝轻点',
    tag: '#2PU8GLGGL',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png',
      large: 'https://api-assets.clashofclans.com/badges/512/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/qSKNpYBxc6bIsAETpUY3VZtd_-QkR9-HA7pMX8qYqsE.png'
    },
    stars: 8,
    destructionPercentage: 17.8
  },
  {
    state: 'warEnded',
    name: 'Trip to Walhall',
    tag: '#QPYC0QYJ',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png',
      large: 'https://api-assets.clashofclans.com/badges/512/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/cKzBkIoyooJUnuLkleTZf74RKPmp4eS2G_5y7zyKeyo.png'
    },
    stars: 55,
    destructionPercentage: 100
  },
  {
    state: 'warEnded',
    name: 'blue vapor',
    tag: '#GJ98C29G',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png',
      large: 'https://api-assets.clashofclans.com/badges/512/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/1C3HEUj_Jj0-TgKmxHinQzjss5qZ0Js0nGwFs60ICjk.png'
    },
    stars: 28,
    destructionPercentage: 68.73333333333333
  },
  {
    state: 'warEnded',
    name: 'Air Hounds 3',
    tag: '#2P9UG82L',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png',
      large: 'https://api-assets.clashofclans.com/badges/512/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/VZWxMkdj9ayVb1eyrES7P4y_xBo9ZA_xxxxEt2KU9zc.png'
    },
    stars: 39,
    destructionPercentage: 78.33333333333333
  },
  {
    state: 'warEnded',
    name: '雨过天晴',
    tag: '#R8JRUJCU',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png',
      large: 'https://api-assets.clashofclans.com/badges/512/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/oMZcD81pCMfJIzoOTT1OxsfaeRDRJyYxPB7IqrZcpfk.png'
    },
    stars: 32,
    destructionPercentage: 76.6
  },
  {
    state: 'warEnded',
    name: '青春无敌',
    tag: '#9LLVCP9V',
    badgeUrls: {
      small: 'https://api-assets.clashofclans.com/badges/70/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png',
      large: 'https://api-assets.clashofclans.com/badges/512/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png',
      medium: 'https://api-assets.clashofclans.com/badges/200/Z8Z1Er0uyqqaTAyDqsIh21SoIfs9WWFvmUgw8vH8jcw.png'
    },
    stars: 44,
    destructionPercentage: 80.6
  }
]


let reduced = {}

list.forEach(player => {
    if(!reduced[player.tag]){
    reduced[player.tag] = player
  }else{
    reduced[player.tag].stars  =player.stars
    reduced[player.tag].destructionPercentage  = player.destructionPercentage
  }
})

console.log(`${list.length} objects reduced to ${Object.keys(reduced).length}`)
console.log(reduced)

  • Related