Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
How to loop through a javascript object and sum amounts in a new object

I have a javascript object

const data = {
  type: "FeatureCollection",
  features: [{
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [-118.4158548, 33.9811315]
    },
    properties: {
      name: "John Smith",
      total_due: 1
    }
  }, {
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [-122.4605458, 37.7286008]
    },
    properties: {
      name: "June Waters",
      total_due: 50
    }
  }, {
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [-122.4120835, 37.7778926]
    },
    properties: {
      name: "John Smith",
      total_due: 2
    }
  }, {
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [-118.3508279, 34.0701918]
    },
    properties: {
      name: "June Waters",
      total_due: 60
    }
  }, {
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [-82.4000143, 34.8518747]
    },
    properties: {
      name: "Mike Jones",
      total_due: 30
    }
  }]
};

What I'd like to do is loop through the object, add the unique names to a new object and sum the totals. The new object would look like this:

newObject = {'June Waters':110, 'Mike Jones':30, 'John Smith': 3}

I tried to do the following but I'm so new to JS so I don't know what I'm doing.

var newObject = {}
  
data.features.forEach(function (item) {
  newObject.item.properties.name = newObject.item.properties.name + item.properties.total_due
});

The following is a portion of the error that is generated.

TypeError: Cannot read properties of undefined (reading 'properties')
    at c:\Users\ryanj\z\const data =.js:10:18
    at Array.forEach (<anonymous>)....

Any help or hints would be truly appreciated.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

you ran into an error because newObject.item is undefined since empty object therefore any property you are trying to access beyond that such as properties is not found

by doing some tweaks to your implementation. I'm adding the name to the newObject if it already doesn't exist then i proceed to add the totals

const data = {"type": "FeatureCollection","features": [{"type": "Feature","geometry": {"type": "Point","coordinates": [-118.4158548, 33.9811315]},"properties": {"name": "John Smith","total_due": 1}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-122.4605458, 37.7286008]},"properties": {"name": "June Waters","total_due": 50}},{"type":"Feature","geometry": {"type": "Point","coordinates": [-122.4120835, 37.7778926]},"properties": {"name": "John Smith","total_due": 2}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-118.3508279, 34.0701918]},"properties": {"name": "June Waters","total_due": 60}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-82.4000143, 34.8518747]},"properties": {"name": "Mike Jones","total_due": 30}}]};

var newObject = {}
  
data.features.forEach(function (item) {
    if(!newObject[item.properties.name])newObject[item.properties.name] = 0;
  newObject[item.properties.name] = newObject[item.properties.name] + item.properties.total_due
});

console.log(newObject)

similarly you can use reduce for these type of problems

const data = {"type": "FeatureCollection","features": [{"type": "Feature","geometry": {"type": "Point","coordinates": [-118.4158548, 33.9811315]},"properties": {"name": "John Smith","total_due": 1}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-122.4605458, 37.7286008]},"properties": {"name": "June Waters","total_due": 50}},{"type":"Feature","geometry": {"type": "Point","coordinates": [-122.4120835, 37.7778926]},"properties": {"name": "John Smith","total_due": 2}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-118.3508279, 34.0701918]},"properties": {"name": "June Waters","total_due": 60}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-82.4000143, 34.8518747]},"properties": {"name": "Mike Jones","total_due": 30}}]};

let total = data.features.reduce((acc,curr) =>{
    if(!acc[curr.properties.name])acc[curr.properties.name]=0;
  acc[curr.properties.name]+=curr.properties.total_due;
  return acc;
},{})

console.log(total)

about 4 years ago · Juan Pablo Isaza Relatório

0

In Javascript, you can refer to an object properties using the square brackets [] just like what we do in an array.

In this code, firstly we check whether the key is already defined in the object. If it is not defined, we will simply assign the value of total_due to the key (which in this case is the name of the person). In the next iteration, if Javascript recognises the key and the value of the key is not undefined, we simply add the total_due value to the current key value.

let finalData = {}
data.features.forEach((item) => {
    if (finalData[item.properties.name] === undefined) {
        finalData[item.properties.name] = item.properties.total_due;
    }
    else {
        finalData[item.properties.name] += item.properties.total_due;
    }
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda