2
The following code will return me the following result:
/**
* Dados
*/
let data =
[
{ data: "01/01/2021", value1: "21", value2: "98" },
{ data: "02/01/2021", value3: "22", value4: "99"},
{ data: "03/01/2021", value5: "36", value6: "100" }
];
/**
* O header são as keys
* Pegando apenas os values e inserindo numa lista vázia
*/
let values = [];
values.push(Object.keys(data[0]));
function getValues(data) {
let arr = [];
for (let i = 0; i < data.length; i++) {
arr.push(Object.values(data[i]));
}
return arr;
}
values = values.concat(getValues(data));
console.log(values);
[
[ 'data', 'value1', 'value2' ],
[ '01/01/2021', '21', '98' ],
[ '02/01/2021', '22', '99' ],
[ '03/01/2021', '36', '100' ]
]
I need to turn into Whole all values of columns "value1" and "value2". How to obtain only the values of the columns "value1" and "value2" ?
You want to modify the table (converting types) or only returning a new array with all values in the numeric type?
– Luiz Felipe
It is worth remembering that
Object.values
does not guarantee any specific output order, so there may be unexpected behavior depending on some situations.– Luiz Felipe
return a new array with all values in the numeric type. I am aware of the sort.
– Gabriel Menezes
@Luizfelipe.
Object.values
guarantees yes the output order which is the same order as the original insertion order. It is documentedaqui
that The Object.values() method returns an array with the values of the properties of a given object, in the same order provided by the for...in loop and here The for...in loop interacts over enumerated properties of an object in the original insertion order– Augusto Vasques
@Augustovasques, it seems that it is something recent then, because several sources indicate that the order is not guaranteed (and in fact it is what I thought). See here, here, etc... Generally speaking, seeing now, I think from the ES6, the order is indeed guaranteed. But I’m still looking for references in the spec.
– Luiz Felipe
See Finding in Ideone. cc: @Augustovasques - I confess I found it strange... But from the JS, we couldn’t expect less, right? : P
– Luiz Felipe
@Luizfelipe, It’s the order of
for...in
but in a practical sense it really has no way to rely on the return order ofObject.values
because if there are numeric keys he puts them in front.– Augusto Vasques