0

Given I have this Json:

{
  "REGIÃO": "BH CENTRO NORTE HPF",
  "TIPO DE VERBA": "VISIBILIDADE",
  "VALOR PLANEJADO": "R$1,452.00",
  "VALOR APURADO": "R$0.00",
  "SALDO FINAL": "R$1,452.00"
},
{
  "REGIÃO": "BH KEY ACCOUNT HPF",
  "TIPO DE VERBA": "OFERTA FÍSICA",
  "VALOR PLANEJADO": "R$54.74",
  "VALOR APURADO": "R$0.00",
  "SALDO FINAL": "R$54.74"
},
{
  "REGIÃO": "BH KEY ACCOUNT HPF",
  "TIPO DE VERBA": "REBAIXA DE PREÇO OFF",
  "VALOR PLANEJADO": "R$57,456.88",
  "VALOR APURADO": "R$0.00",
  "SALDO FINAL": "R$57,456.88"
},
{
  "REGIÃO": "BH KEY ACCOUNT HPF",
  "TIPO DE VERBA": "VISIBILIDADE",
  "VALOR PLANEJADO": "R$563,651.25",
  "VALOR APURADO": "R$0.00",
  "SALDO FINAL": "R$563,651.25"
}

I would like to get a set of the values for key "REGIÃO", so for this example I would get this:

[BH CENTRO NORTE HPF, BH KEY ACCOUNT HPF]

How do I do this withoug using a foreach and pushing values to an array and then removing duplicates?

3
  • That's invalid json. There's no containing array or object Commented Jun 16, 2017 at 14:45
  • Mmh... All these high level frameworks tend to make people forget that most of these helper functions will be using a foreach loop without telling :) Commented Jun 16, 2017 at 14:47
  • By the way, a solutions could be using regex to identify the property name and retrieve the property value. I did it one time, took me 2 hours just to understand how regex worked and debugging it, so if you want to get into it, I'' leave you to it! Regex has options to recognize and return values when it recognizes patterns. You would look for the pattern: ' "TARGETPROPERTY": "VALUETORETURN" ' and with the proper keywords you can get that value to be returned Commented Jun 16, 2017 at 14:50

1 Answer 1

1

Assuming these objects are contained within an array, you can do:

const data = [{
  "REGIÃO": "BH CENTRO NORTE HPF",
  "TIPO DE VERBA": "VISIBILIDADE",
  "VALOR PLANEJADO": "R$1,452.00",
  "VALOR APURADO": "R$0.00",
  "SALDO FINAL": "R$1,452.00"
},
{
  "REGIÃO": "BH KEY ACCOUNT HPF",
  "TIPO DE VERBA": "OFERTA FÍSICA",
  "VALOR PLANEJADO": "R$54.74",
  "VALOR APURADO": "R$0.00",
  "SALDO FINAL": "R$54.74"
},
{
  "REGIÃO": "BH KEY ACCOUNT HPF",
  "TIPO DE VERBA": "REBAIXA DE PREÇO OFF",
  "VALOR PLANEJADO": "R$57,456.88",
  "VALOR APURADO": "R$0.00",
  "SALDO FINAL": "R$57,456.88"
},
{
  "REGIÃO": "BH KEY ACCOUNT HPF",
  "TIPO DE VERBA": "VISIBILIDADE",
  "VALOR PLANEJADO": "R$563,651.25",
  "VALOR APURADO": "R$0.00",
  "SALDO FINAL": "R$563,651.25"
}]

const newData = data.map(obj => obj["REGIÃO"]).filter((v, i, self) => i == self.indexOf(v))

console.log(newData)

The map selects only the values for the REGIÃO key from each object and puts them into and array. The following filter removes the duplicates by checking if the index of the element is the same as the index of its first occurrence. If they aren't equal, it removes it from the array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.