return values of a javascript object

Asked

Viewed 165 times

3

I have this object in which I need to take the largest portion with the rate 0 (without interest) and its value, and also take the largest number of installment available and its value.. I’ve tried it many ways and I couldn’t, someone can help me?

const obj = {
    "hipercard": {
        "total_amount": 187.38,
        "installments": {
            "1": {
                "value": 119.9,
                "rate": 0
            },
            "2": {
                "value": 59.95,
                "rate": 0
            },
            "3": {
                "value": 39.97,
                "rate": 0
            },
            "4": {
                "value": 29.98,
                "rate": 0
            },
            "5": {
                "value": 23.98,
                "rate": 0
            },
            "6": {
                "value": 27.53,
                "rate": 10
            },
            "7": {
                "value": 24.63,
                "rate": 10
            },
            "8": {
                "value": 22.47,
                "rate": 10
            },
            "9": {
                "value": 20.82,
                "rate": 10
            }
        }
    },
      "Amex": {
        "total_amount": 187.38,
        "installments": {
            "1": {
                "value": 119.9,
                "rate": 0
            },
            "2": {
                "value": 59.95,
                "rate": 0
            },
            "3": {
                "value": 39.97,
                "rate": 0
            },
            "4": {
                "value": 29.98,
                "rate": 10
            },
            "5": {
                "value": 23.98,
                "rate": 10
            },
            "6": {
                "value": 27.53,
                "rate": 10
            },
            "7": {
                "value": 24.63,
                "rate": 10
            },
            "8": {
                "value": 22.47,
                "rate": 10
            },
            "9": {
                "value": 20.82,
                "rate": 10
            }
        }
    }
}
  • 1

    What you call "parcel"?

  • parcels are keys within installments, "1", "2" ...

  • So you want to know value greater with the rate: 0 and also the value of the largest share (would be the share 9) regardless of your rate correct? what names you want to give them?

  • yes, more not always the value of the maximum part will be 9, it can come 12x for example... parcela_sem_juros, valor_sem_juros || parcela_sem_juros, valor_com_juros Thanks!

3 answers

3


You can use the Math.max to know the highest index. Using the spread operator you can pass all plots or filtered by rate === 0 and know the highest index.

const obj = {"hipercard":{"total_amount":187.38,"installments":{"1":{"value":119.9,"rate":0},"2":{"value":59.95,"rate":0},"3":{"value":39.97,"rate":0},"4":{"value":29.98,"rate":0},"5":{"value":23.98,"rate":0},"6":{"value":27.53,"rate":10},"7":{"value":24.63,"rate":10},"8":{"value":22.47,"rate":10},"9":{"value":20.82,"rate":10}}},"Amex":{"total_amount":187.38,"installments":{"1":{"value":119.9,"rate":0},"2":{"value":59.95,"rate":0},"3":{"value":39.97,"rate":0},"4":{"value":29.98,"rate":10},"5":{"value":23.98,"rate":10},"6":{"value":27.53,"rate":10},"7":{"value":24.63,"rate":10},"8":{"value":22.47,"rate":10},"9":{"value":20.82,"rate":10}}}};

const parcelas = obj.hipercard.installments;

const indexMaisAlto = Math.max(...Object.keys(parcelas));
const parcelaMaisAlta = parcelas[indexMaisAlto];

const indexSemJurosMaisAlto = Math.max(
  ...Object
    .keys(parcelas)
    .filter(index => parcelas[index].rate === 0)
);
const parcelaSemJuroMaisAlta = parcelas[indexSemJurosMaisAlto];

console.log('parcelaMaisAlta', indexMaisAlto, parcelaMaisAlta);
console.log('parcelaSemJuroMaisAlta', indexSemJurosMaisAlto, parcelaSemJuroMaisAlta);

  • Definitely your solution is more elaborate and elegant, but what if Amex has more installments or less interest? In your solution you are considering only the Hyper...

  • @Jduwe to avoid "over Engineering", ie optimize in the void without having a case to apply, I gave the answer as is. If there are more cases the answer can be modified. Your answer is also limited to 2 levels of depth of an object, and you don’t know if all objects respect that.

  • My answer respects the example.

  • That’s right, mine too :)

1

I would do it this way... It’s not so elegant, but it’s simple and efficient.

const obj = {"hipercard":{"total_amount":187.38,"installments":{"1":{"value":119.9,"rate":0},"2":{"value":59.95,"rate":0},"3":{"value":39.97,"rate":0},"4":{"value":29.98,"rate":0},"5":{"value":23.98,"rate":0},"6":{"value":27.53,"rate":10},"7":{"value":24.63,"rate":10},"8":{"value":22.47,"rate":10},"9":{"value":20.82,"rate":10}}},"Amex":{"total_amount":187.38,"installments":{"1":{"value":119.9,"rate":0},"2":{"value":59.95,"rate":0},"3":{"value":39.97,"rate":0},"4":{"value":29.98,"rate":10},"5":{"value":23.98,"rate":10},"6":{"value":27.53,"rate":10},"7":{"value":24.63,"rate":10},"8":{"value":22.47,"rate":10},"9":{"value":20.82,"rate":10}}}};

let maiorParcela = 0;
let valor = 0;
let maiorParcelaSemJuros = 0;
let valorSemJuros = 0;
for (let operadora in obj) {
	for (let parcela in obj[operadora].installments) {
		if (parseInt(parcela) > maiorParcela) {
			maiorParcela = parcela;
			valor = obj[operadora].installments[parcela].value;
		}
		if (obj[operadora].installments[parcela].rate === 0 && parseInt(parcela) > maiorParcelaSemJuros) {
			maiorParcelaSemJuros = parcela;
			valorSemJuros = obj[operadora].installments[parcela].value;
		}
	}
}
console.log(`Em até ${maiorParcelaSemJuros}x de R$${valorSemJuros} sem juros.`);
console.log(`Em até ${maiorParcela}x de R$${valor}.`);

  • I did a similar scope and I believe I was wrong in validations.. has helped me too much already!

0

could catch them by the position, for example:

obj.hipercard.installments[1]

will return the one Object with value and rate, if you only want value or rate

obj.hipercard.installments[1].value
obj.hipercard.installments[1].rate

so will pick up separately, hope to have helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.