简体   繁体   English

从对象数组中提取数据

[英]Extract data from an array of Object

I have this array of Object that I am getting from my database: 我有从数据库中获取的对象数组:

[Array of Object][1] [对象数组] [1]

I would like to make an array of array for each value, but I can't manage to find a way to do it as I'm a beginner of javascript. 我想为每个值创建一个数组数组,但是由于我是javascript的初学者,因此无法设法找到一种方法。

For example : 例如 :

var Stats=[
  [39,49,43,42,41,35], //SGW Value for each Object
  [37,44,49,46,52,42], //UD Value for each Object 
  [8,11,8,8,16,15],    //Virtual Value for each Object
  ...     
]

The goal is to make a chart on chart.js that look like that : 目的是在chart.js上制作一个如下所示的图表:

[Chart Goal][2] [图表目标] [2]

I would need to loop the dataset because I'll add more data and it would be way too long to set each dataset individually. 我将需要循环数据集,因为我将添加更多数据,并且单独设置每个数据集的时间太长了。

Thanks for your time. 谢谢你的时间。

You can do it like this: 您可以这样做:

 let array1 = [ { param1: 10, param2: 20 }, { param1: 30, param2: 40 } ] let array2 = array1.map(item => Object.values(item)); console.log(array2); // prints [[10, 20], [30, 40]] 

First of all you need to create an array for each property you want to plot; 首先,您需要为要绘制的每个属性创建一个数组; ie: 即:

var fsp = [],
    msg = [],
    sgw = [];

Then you can loop over your dataset and put the data in each array: 然后,您可以遍历数据集并将数据放入每个数组中:

yourArray.forEach(function(obj){
    //obj takes the value of each object in the database
    fsp.push(obj.fsp);
    msg.push(obj.msg);
    sgw.push(obj.sgw);
})

or, if you are more familiar with for loop 或者,如果您更熟悉for循环

for(var obj of yourArray){
    fsp.push(obj.fsp);
    msg.push(obj.msg);
    sgw.push(obj.sgw);
}

Finally you can create an array as you pointed in your example 最后,您可以按照示例中的说明创建一个数组

var result = [];

result.push(fsp, msg, sgw);

And the result will be 结果将是

[
    [89, 59, 43, 60, 81, 34, 28, 58, 75, 41],
    [77, 91, 4, 56, 6, 1, 42, 82, 97, 18],
    [24, 34, 4, 13, 75, 34, 14, 41, 20, 38]
]

For more informations take a look at Array.forEach() , Array.push() and for...of documentations 有关更多信息,请Array.forEach()Array.push()for...of文档

EDIT 编辑

As you pointed in your comment, you can generate arrays dynamically creating an object like var arrays = {}; 正如您在评论中指出的那样,您可以动态生成数组,从而创建对象,例如var arrays = {}; . Then in forEach() , or if for...of , you need to loop over objects with a for...in loop. 然后在forEach() ,或者在for...of ,您需要使用for...in循环遍历对象。 The variable you declare in loop's head takes the value of index, numeric for Arrays, literal for Objects. 您在循环头中声明的变量采用index的值,对于Arrays为数字,对于Objects为文字。 You have to do something like: 您必须执行以下操作:

yourArray.forEach(function(obj){
    for(let index in obj){
        if(!arrays[index]) // check if property has already been set and initialized
            arrays[index] = []; // if not, it's initialized

        arrays[index].push(obj[index]) // push the value into the array
    }
})

Note that Object has been treated as Array because you access its properties with a variable filled at runtime. 请注意,对象已被视为数组,因为您在运行时使用填充的变量访问其属性。

The result will be: 结果将是:

arrays = {
    fsp: [89, 59, 43, 60, 81, 34, 28, 58, 75, 41],
    msg: [77, 91, 4, 56, 6, 1, 42, 82, 97, 18],
    sgw: [24, 34, 4, 13, 75, 34, 14, 41, 20, 38]
}

To obtain only arrays use Object.values() . 要仅获取数组,请使用Object.values()

If you cannot imagine how this works, I suggest you to make some examples in Chrome Developer Tools' console, or in Node's console, or wherever you can have a realtime feedback, putting in the middle of code some console.log() of variables 如果您无法想象这是如何工作的,建议您在Chrome开发者工具的控制台或Node的控制台中,或者在有实时反馈的任何地方举一些例子,在代码中间放置一些console.log()变量

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

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