简体   繁体   English

console.log返回[object Object]

[英]console.log returns [object Object]

I'm starting with javascript and I have this problem I'm trying to return and array with the data but when I console log I get "[object Object][object Object]" 我从javascript开始,遇到这个问题,我试图返回数据并按数组排列,但是当我在控制台日志中得到“ [object Object] [object Object]”

this is the data I have to work with 这是我必须处理的数据

var MARKDATA = {
 "SOR": {
   "mark": [
    {
     "data": {
       "2015": 21.680000,
       "2016": 23.739999,
       "2017": 23.760000
     },
     "markName": "swimming",
     "markCode": "SWM"
   },
   {
    "data": {
       "2015": "",
       "2016": 61.55429840,
       "2017": 61.482299804
     },
     "markName": "running (time)",
     "indicatorCode": "RM"
   }
  ]
 } 
};

this is what I tried 这就是我尝试过的

const valuesArr= MARKDATA.SOR.mark;

let acum= '';
const showData = arr => {
 const dataArr= arr.map(value => value.data).join('') 
 acum += dataArr;
};
showData(valuesArr);

console.log(acum)

I want that console log returns and array with the years and the values of the object data, what I want to do is use this array to calculate with the reduce method the average of each data. 我希望控制台日志返回并包含年份和对象数据的值的数组,我要做的是使用此数组通过reduce方法计算每个数据的平均值。

Or if someone knows a better way to get the average of the values of the object data I will apreciate it 或者,如果有人知道一种更好的方法来获取对象数据的平均值,我将对其进行说明

Your current code is building a string, explicitly: 您当前的代码正在显式构建一个字符串:

  • You initialize acum with a string ( '' ) 您初始化acum有一个字符串( ''
  • You call join('') (which creates a string) on the result of the map operation 您在map操作的结果上调用join('') (创建一个字符串)

Since map is creating an array of objects, calling join on taht array implicitly calls toString on those objects. 由于map正在创建对象数组,因此在taht数组上调用join隐式在那些对象上调用toString An object that doesn't have special toString behavior returns "[object Object]" in that case. 在这种情况下,不具有特殊toString行为的"[object Object]"将返回"[object Object]"

If you want an array of the data contents, you need to work at the array level: 如果需要data内容的数组,则需要在数组级别进行操作:

let acum = []; // <== An array
const showData = arr => {
    const dataArr= arr.map(value => value.data); // <== No join('')
    acum.push(...dataArr); // <== Appending to the array
};
showData(valuesArr);

console.log(acum);

That's the minimal changes version, but there's no reason to create those temporary arrays (the return values of map ), and there's no reason for the showData function. 那是最小的更改版本,但是没有理由创建这些临时数组( map的返回值),也没有理由使用showData函数。 Just use a for-of loop. 只需使用for-of循环。 You can also include destructuring. 您还可以包括解构。 So: 所以:

let acum = [];
for (const {data} of valuesArr) {
    acum.push(...data);
}

console.log(acum);

You can shoehorn that into a reduce (because you can shoehorn any array operation into a reduce ), but it doesn't buy you anything: 您可以将其转换为reduce (因为您可以将任何数组操作转换为reduce ),但是它并不能为您带来任何收益:

let acum = valuesArr.reduce((a, {data}) => {
    a.push(...data);
    return a;
}, []);

console.log(acum);

Alternately (but this still pointlessly creates an extra array) you could use the proposed-but-still-Stage-3 Array.prototype.flat : 或者,(但这仍然毫无意义地创建了一个额外的数组),您可以使用Array.prototype.flat -but-still-Stage-3 Array.prototype.flat

let acum = valueArr.map(({data}) => data).flat();

console.log(acum);

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

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