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

119
Visualizações
Turn a array of object with a nested object inside into a new array with values from the nested object

I am trying to turn a array that exists of objects with a nested array inside it into a new array that consist of the values inside the nested array separately(Might sound very complicated but example show very clear)

What I have:

const values = [{
    geometry: {
      "coordinates": [11.4828, 59.1264],
      "Type": "point"
    },
    properties: {
      Region: "Oklahoma",
      sales: {
        donuts: {
          2005: 5,
          2006: 8,
          2007: 10
        },
        apples: {
          2005: 10,
          2006: 8,
          2007: 10
        }
      }
    }
  },
  {
    geometry: {
      "coordinates": [9.4828, 76.1264],
      "Type": "point"
    },
    properties: {
      Region: "Texas",
      sales: {
        donuts: {
          2005: 8,
          2006: 0,
          2007: 7
        },
        apples: {
          2005: 7,
          2006: 9,
          2007: 4
        }
      }
    }
  }
]
const filterValue = "donuts"

What My goal is:

newValues =[{geometry: {"coordinates": [11.4828, 59.1264],"Type":"point"},properties:{Region: "Oklahoma",value:5,time:2005}},{geometry: {"coordinates": [11.4828, 59.1264],"Type":"point"},properties:{Region: "Oklahoma",value:8,time:2006}},{geometry: {"coordinates": [11.4828, 59.1264],"Type":"point"},properties:{Region: "Oklahoma",value:10,time:2007}} AND SO ON FOR ALL THE values that are in const donuts/filter for each value. Could also switch out value for donuts if that is easier
}
]

What I have tried so far:

const newObject = []
values.map((value)=>{
  console.log(value.properties.sales.donuts)
  for (var key in value.properties.sales.donuts) {
        if (value.properties.sales.donuts.hasOwnProperty(key)) {
            newObject.push({...value,value.properties:{[key]:value.properties.sales.donuts[key]})
        }
    }
})
console.log(newObject)
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Your current syntax is invalid - value.properties: { is not a valid key in an object literal. Since you want the output properties property to contain data corresponding to the region and year too, you should include those in the object being pushed. Since you also don't need the year to be a key in the new object, but a value (with a value property), the object being pushed should reflect that.

You should only use .map when you're returning a value inside the mapper function to create a new array. If you're iterating only to perform side-effects, you should use a more appropriate generic iteration method, like for..of or forEach. In this case, since you want to create multiple objects in the output array from one associated object in the input array, .flatMap would be more appropriate.

Map the .donuts entries and return the new object with geometry and properties properties.

const values=[{geometry:{coordinates:[11.4828,59.1264],Type:"point"},properties:{Region:"Oklahoma",sales:{donuts:{2005:5,2006:8,2007:10},apples:{2005:10,2006:8,2007:10}}}},{geometry:{coordinates:[9.4828,76.1264],Type:"point"},properties:{Region:"Texas",sales:{donuts:{2005:8,2006:0,2007:7},apples:{2005:7,2006:9,2007:4}}}}];

const filterValue = "donuts";
const output = values.flatMap(({ geometry, properties }) =>
    Object.entries(properties.sales[filterValue]).map(([year, saleCount]) => ({
        geometry,
        properties: {
            Region: properties.Region,
            value: saleCount,
            time: year,
        }
    }))
);
console.log(output);

about 4 years ago · Juan Pablo Isaza Relatório

0

You'll need a flatmap here

const values = [
  {
    geometry: {
      coordinates: [11.4828, 59.1264],
      Type: 'point',
    },
    properties: {
      Region: 'Oklahoma',
      sales: {
        donuts: {
          2005: 5,
          2006: 8,
          2007: 10,
        },
        apples: {
          2005: 10,
          2006: 8,
          2007: 10,
        },
      },
    },
  },
  {
    geometry: {
      coordinates: [9.4828, 76.1264],
      Type: 'point',
    },
    properties: {
      Region: 'Texas',
      sales: {
        donuts: {
          2005: 8,
          2006: 0,
          2007: 7,
        },
        apples: {
          2005: 7,
          2006: 9,
          2007: 4,
        },
      },
    },
  },
];
const mapData = (data, filterBy) => {
  return data.flatMap((state) => {
    return Object.entries(state.properties.sales[filterBy]).map(
      ([time, value]) => {
        return {
          ...state,
          properties: {
            region: state.properties.Region,
            time,
            value,
          },
        };
      }
    );
  });
};

const mappedDonuts = mapData(values, 'donuts');
console.log(mappedDonuts);

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