简体   繁体   English

如何将fourier.getEnergy() 的每个元素添加到数组中?

[英]How can I add each element of fourier.getEnergy() into an array?

Trying to make a visualizer, the values in fft.getEnergy() (a p5.js sound library function) returns the amount of volume at each frequency.试图制作一个可视化工具,fft.getEnergy()(一个 p5.js 声音库函数)中的值返回每个频率的音量。 I would like to take the first 1024 of these volumes and store them in an array so I can map each number to a graph.我想取出这些卷中的前 1024 个并将它们存储在一个数组中,这样我就可以将每个数字 map 放到一个图表中。 The problem is when I try to add them to the array, the array stores only 1 value 1024 times and not the first 1024 different values.问题是当我尝试将它们添加到数组时,数组仅存储 1 个值 1024 次,而不是前 1024 个不同的值。

This is a snapshot of the getEnergy() values: https://imgur.com/a/7zSV9DK这是 getEnergy() 值的快照: https://imgur.com/a/7zSV9DK

And this is a snapshot of the array I'm trying to make while trying to push the first 1024 values: https://imgur.com/a/cs3FA0U which shows that it's printing one value 1024 times.这是我在尝试推送前 1024 个值时尝试制作的数组的快照: https://imgur.com/a/cs3FA0U这表明它正在打印一个值 1024 次。

var arrays = []
    for(var i = 0; i < 1024; i++)
    {       

        arrays.push(energy)  //energy = fft.getEnergy("bass")
       
    }

  
   console.log(arrays)

From what I read on this page https://p5js.org/reference/#/p5.FFT/getEnergy根据我在此页面上阅读的内容https://p5js.org/reference/#/p5.FFT/getEnergy

getEnergy("bass") return a "predefined frequency ranges" which I guess is an array of energies of predefined frequencies from 0 to bass max limit . getEnergy("bass")返回一个“预定义的频率范围”,我猜它是从0bass max limit的预定义频率的能量数组。

What you do in your for loop is pushing the result array of frequencies to your result array 1024 times (an not 1024 individual values as you want).您在for循环中所做的是将频率结果数组推送到结果数组 1024 次(而不是您想要的 1024 个单独的值)。

so what your are trying to do is所以你想要做的是

const resultArray = [];

for (let i=0; i<1024; i++) {
  resultArray.push(energy[i]);
}

console.log(resultArray);

(note that I don't use var . I Suggest that you look at the difference between var | let | const and that you learn about the notion of scope too.) (请注意,我不使用var 。我建议您查看var | let | const之间的区别,并且您也了解 scope 的概念。)

Yet this code above is not quite good either.然而,上面的这段代码也不是很好。 I suggest you look at what the Array object in JS is and look at all the ways to manipulate it.我建议你看看 JS 中的Array object 是什么,并查看所有操作它的方法。 Using the slice method would be better.使用slice方法会更好。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

const resultArray = energy.slice(1024);

And then to sort your array you can use the sort method https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort然后对数组进行排序,您可以使用sort方法https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

(Though be careful that "the default sort order is ascending, built upon converting the elements into strings") (虽然要注意“默认排序顺序是升序,建立在将元素转换为字符串的基础上”)

暂无
暂无

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

相关问题 如何向对象添加数组,但是为每个数组元素过滤除了一列以外的所有数组? - How can I add an array to an object but filter out all but one column for each array element? 如何将数组 1 的总和添加到数组 2 中的每个元素 - How do I add the sum of array 1 to each element in array 2 如何从网页获取JSON对象数组并将每个元素添加到HTML中的行? - How can i get an array of JSON object from a webpage and add each element to the row in HTML? 如何将另一个元素添加到数组中? - How can I add another element to an array? 如何使用javascript或angular将一个数组的每个元素添加到其他数组中的对应元素 - How can I add each element of one array to corresponding elements in other arrays with javascript or angular 如何使用Javascript Fetch API将超链接添加到返回数组的每个元素? - using Javascript fetch api how can I add a hyperlink to each element o f returned array? 我如何在正确的数组中添加元素 - How i can add element in correct array 如何将 class 添加到 React 组件数组中的每个元素? - How do I add a class to each element in an array of React Components? 如何将每个数组元素添加到aN数组中? - How to add each array element into aN array? 如何解析jQuery中的JSON数组,可视化页面上的每个元素,并对每个要分割的元素进行选择? - How can I parse a JSON array in jQuery, visualize each element on a page with an option on each element to be sected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM