简体   繁体   English

JavaScript:从键值数组中提取特定键的值并将它们存储在新数组中

[英]JavaScript: Extract values of a specific key out of key-value array and store them in a new array

I have an' multidimensional key-value array like this:我有一个像这样的多维键值数组:

 '[[ {"_time":"2022-01-03T00:00:00Z","_value":10.70140000000014,"ts":1641168000000}, {"_time":"2022-01-04T00:00:00Z","_value":13.002499999999767,"ts":1641254400000}, {"_time":"2022-01-05T00:00:00Z","_value":16.171700000000182,"ts":1641340800000}, {"_time":"2022-01-06T00:00:00Z","_value":17.48929999999981,"ts":1641427200000}, ]]'

and I need the values from each of them in a new variable in this way:我需要以这种方式将它们中的每一个的值放在一个新变量中:

 [10.70140000000014,13.002499999999767,16.171700000000182,17.48929999999981]

I tried it with map but that do not match my needs:我用map试过,但这不符合我的需求:

 function getPreparedData(data) { if (data) { return data[0].map(elm => ({ a: elm._value })); } return []; };

How can I solve this?我该如何解决这个问题?

Thanks谢谢

Frank坦率

In your example, the function getPreparedData is returning array of objects.在您的示例中, function getPreparedData正在返回对象数组。 You actually want to return an array of values (where values are just the value of the _value property)您实际上想要返回一个值数组(其中值只是_value属性的值)

 const data = [[ {"_time":"2022-01-03T00:00:00Z","_value":10.70140000000014,"ts":1641168000000}, {"_time":"2022-01-04T00:00:00Z","_value":13.002499999999767,"ts":1641254400000}, {"_time":"2022-01-05T00:00:00Z","_value":16.171700000000182,"ts":1641340800000}, {"_time":"2022-01-06T00:00:00Z","_value":17.48929999999981,"ts":1641427200000}, ]]; function getPreparedData(data) { return data[0].map(elm => elm._value) }; const res = getPreparedData(data); console.log(res);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM