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

126
Visualizações
Copy object which has a specific value

I have a scenario where i need to copy the offers (without price) if the price is greater than 0 from the following json:

const data = [
    {
        "id": "offer1",
        "Identifier": {
            "info": "some info for offer1",
            "value": "some string for offer1"
        },
        "Price": {
            "Total": 94.30
        }
    },  
    {
        "id": "offer2",
        "Identifier": {
            "info": "some info for offer2",
            "value": "some string for offer2"
        },
        "Price": {
            "Total": 0.0
        }
    },
    {
        "id": "offer3",
        "Identifier": {
            "info": "some info for offer3",
            "value": "some string for offer3"
        },
        "Price": {
            "Total": 48.50
        }
    }
];

I need to get all the offers where the price is > 0 and put them into another array with a specific format, the expected result should look like this:

const result = [
  {
    "id":"offer1",
    "reference":"offer1",
    "Identifier":{
      "info":"some info for offer1",
      "value":"some string for offer1"
    }
  },
  {
    "id":"offer3",
    "reference":"offer3",
    "Identifier":{
      "info":"some info for offer3",
      "value":"some string for offer3"
    }
  }
]

The first step I made was to filter the initial data array to exclude offers with a Total = 0.

const filteredOffers = data.filter(offer => offer.Price.Total > 0);

What should I do next to get the expected result? Thanks.

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

0

You could map new objects.

const
    data = [{ id: "offer1", Identifier: { info: "some info for offer1", value: "some string for offer1" }, Price: { Total: 94.3 } }, { id: "offer2", Identifier: { info: "some info for offer2", value: "some string for offer2" }, Price: { Total: 0 } }, { id: "offer3", Identifier: { info: "some info for offer3", value: "some string for offer3" }, Price: { Total: 48.5 } }],
    result = data
        .filter(({ Price: { Total } }) => Total > 0)
        .map(({ Price, ...o }) => ({ ...o, reference: o.id }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the reduce method to map over the items and only return those with a price not equal to 0.

 function formatOffers(data) {
  return data.reduce((acc, item) => {
    const { id, Identifier, Price } = item;

    if (Price.Total === 0) {
      return acc;
    } else {
      return [...acc, { id, reference: id, Identifier, Price }];
    }
  }, []);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The next step can be:-

const offersWithoutPrice = filteredOffers.map(offer => {
    delete offer["Price"]; 
    return offer;
});
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