0

I need to get the minimum and maximum values ​​of each of these objects within the following array. I've tried everything I know but the returned value is undefined, how can I do this? NOTE: Afterwards, I need to do the same in a constructor function and in a factory function. Is it the same procedure?

 let faixas3 = [
 {tooltip: 'de R$ 6667 até R$ 7777', minimo: 6667, maximo: 7777},
 {tooltip: 'de R$ 7778 até R$ 8889', minimo: 7778, maximo: 8889},
 {tooltip: 'de R$ 9000 até R$ 10000', minimo: 9000, maximo: 10000}
];
const {tooltip, minimo, maximo} = faixas3;
console.log({tooltip, minimo, maximo}); // { tooltip: undefined, minimo: undefined, maximo: undefined }
console.log(tooltip, minimo, maximo); // undefined undefined undefined
asked Jan 7, 2022 at 11:36
4
  • 1
    use [ ] not {}, const [tooltip, minimo, maximo] = faixas3;, faixas3 is an array not an object. Commented Jan 7, 2022 at 11:37
  • faixas3 is an array, you're using object destructuring syntax. Commented Jan 7, 2022 at 11:38
  • You can use .map(), the result of which will also be an array (with the values that you specified in the map callback function). Or you can use .forEach() if all you want is a "loop" that processes each item. Commented Jan 7, 2022 at 11:47
  • Thank you so much for your help, it was exactly what I needed to do. Commented Jan 7, 2022 at 11:53

3 Answers 3

1

You have an array of objects. To destructure values as you're doing in the example, you have to specify from which index you want to destructure values from.

const {tooltip, minimo, maximo} = faixas3[0];
const {tooltip, minimo, maximo} = faixas3[1];
const {tooltip, minimo, maximo} = faixas3[2];

or you can use a loop for looping over the elements.

answered Jan 7, 2022 at 11:42

3 Comments

Redeclaration of constant variables is not allowed, and overwriting an existing variable doesn't make sense in this case.
yes you have to use var in place of const if you wish to use all three together. I wrote all three together to tell him how he can destructure from each object of the array.
You will definitely need six variables to store the values OP needs.
0

You can try this.

 function column(array, columnName) {
 return array.map(function(value,index) {
 return value[columnName];
 })
} 
const {tooltip, minimo, maximo} = [column(faixas3,'tooltip'),column(faixas3,'minimo'),column(faixas3,'maximo')];
answered Jan 7, 2022 at 11:59

Comments

0

You can 'redefine' faixas3 in several ways using Array.map. Here are two examples. The first map-result is used to declare variables with destructuring.

const logElem = document.querySelector(`pre`);
const faixas3 = [
 {tooltip: 'de R$ 6667 até R$ 7777', minimo: 6667, maximo: 7777},
 {tooltip: 'de R$ 7778 até R$ 8889', minimo: 7778, maximo: 8889},
 {tooltip: 'de R$ 9000 até R$ 10000', minimo: 9000, maximo: 10000}
];
const faixas_minmax1 = faixas3.map(v => [v.minimo, v.maximo]);
logElem.textContent = `faixas_minmax1: ${JSON.stringify(faixas_minmax1)}`;
const faixas_minmax2 = faixas3.map(v => ({min: v.minimo, max: v.maximo}));
logElem.textContent += `\nfaixas_minmax2: ${
 JSON.stringify(faixas_minmax2, null, 2)}`;
// destructuring assignment to variables from faixas_minmax1
const [f3min0, f3max0, f3min1, f3max1, f3min2, f3max2] = faixas_minmax1.flat();
logElem.textContent += `\nf3min0, f3max2: ${f3min0}, ${f3max2}`;
<pre></pre>

answered Jan 7, 2022 at 12:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.