console.log 这个对象的名称和日期

console.log the name and the day of this object

如果我只想 console.log 姓名和日期,如何得到这个:

 [Name1: {
     day: 'Successfully Graphed'
},
 Name2: {
     day: 'Successfully Graphed'
},
 Name3: {
     day: 'Successfully Graphed'
}
 Name4: {
     day: 'Successfully Graphed'
}],

在此对象数组中。

[Name1: {
    day: 'successfully graphed',
    week: 'successfully graphed',
    month: 'successfully graphed',
    year: 'successfully graphed'
},
Name2: {
    day: 'successfully graphed',
    week: 'successfully graphed',
    month: 'successfully graphed',
    year: 'successfully graphed'
},
Name3: {
    day: 'successfully graphed',
    week: 'successfully graphed',
    month: 'successfully graphed',
    year: 'successfully graphed'
},
Name4: {
    day: 'successfully graphed',
    week: 'successfully graphed',
    month: 'successfully graphed',
    year: 'successfully graphed'
}],

我试图通过

得到它

console.log(result.data);

但是当我把它变成

console.log(result.data.name.day);

它将变成未定义的。

将数组 obj 转换为对象,然后尝试它会起作用。

var array = [
    {key:'k1',value:'v1'},
    {key:'k2',value:'v2'},
    {key:'k3',value:'v3'}
];
var mapped = array .map(item => ({ [item.key]: item.value }) );
var newObj = Object.assign({}, ...mapped );
console.log(newObj );

假设 result.data 是您的对象数组:

你得到未定义的原因是 name 在你的结果中不存在(因为对象键实际上称为“Name1”,“Name2”等)。

如果可能(也许超出了问题的范围),我会以这种格式将 result.data 更改为 return:

results.data:
[
 {
    id (or name): <whatever value you want>
    day: 'successfully graphed',
    week: 'successfully graphed',
    month: 'successfully graphed',
    year: 'successfully graphed'
 },
...
],

然后你可以像这样简单地对结果做一个映射:

const mappedResults=results.data.map(nameObject=>({name:id,day}))

如果您无法更改结果的结构,我的建议是按照 .

将数据转换为对象

首先将数据转换为对象数组。

let array = [
    {key:'k1',value:'v1'},
    {key:'k2',value:'v2'},
    {key:'k3',value:'v3'}
];

然后你就可以从这个数组中获取密钥