简体   繁体   English

如何在Javascript中同时访问具有变化值的对象数组中的所有元素?

[英]How to simultaneously access all elements in an array of objects with changing values in Javascript?

My program is constantly collecting data into an array of five objects with two values each: "label" which is a string and "confidence" which is a float:我的程序不断地将数据收集到一个由五个对象组成的数组中,每个对象有两个值:“label”是一个字符串,“confidence”是一个浮点数:

[{"label": vocals, "confidence": 0.98373},{"label": snare, "confidence": 0.32738}...]

This is how teachable machine audio model stores the data.这就是可教机器音频模型存储数据的方式。

I'm trying to change the height of 5 rectangles with the same label names according to the confidence value of each item in the array but I'm having a hard time trying to read and display this data simultaneously, as the order of each label and it's confidence value will change every second or so.我正在尝试根据数组中每个项目的置信度值更改具有相同标签名称的 5 个矩形的高度,但我很难同时读取和显示这些数据,作为每个标签的顺序它的置信度值会每秒左右变化一次。

The program is supposed to be an audio visualizer that is supposed to display the loudness(confidence value) of each instrument at the same time.该程序应该是一个音频可视化器,应该同时显示每个乐器的响度(置信度值)。 I can only think of iterative implementations that would only display one value at a time.我只能想到一次只显示一个值的迭代实现。

  1. Map the data into an object将数据映射到对象

 const values = [{ "label": 'vocals', "confidence": 0.98373 }, { "label": 'snare', "confidence": 0.32738 }] const object = values.reduce((obj, val) => ({ ...obj, [val.label]: val.confidence }), {}) console.log(object)

  1. Sort values by label and use indexes for values.按标签对值进行排序并为值使用索引。

 const values = [{ "label": 'vocals', "confidence": 0.98373 }, { "label": 'snare', "confidence": 0.32738 }].sort((a, b) => a.label.localeCompare(b.label)) console.log(values)

  1. Use .find to find the value you are interested in使用.find查找您感兴趣的值

 const values = [{"label": 'vocals', "confidence": 0.98373},{"label": 'snare', "confidence": 0.32738}] const vocals = values.find(val => val.label === 'vocals') console.log(vocals)

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

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