简体   繁体   English

console.log 这个 object 的名称和日期

[英]console.log the name and the day of this object

How can get this if I want to console.log only the Names and the day just like this:如果我只想控制台记录名称和日期,怎么能得到这个:

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

in this array of objects.在这个对象数组中。

[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'
}],

I tried to get it by我试图得到它

console.log(result.data);

but when I make it as但是当我把它变成

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

it will become undefined.它将变得未定义。

convert array obj to object and then try it will work.将数组 obj 转换为 object 然后尝试它会工作。

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 );

Assuming result.data is an array of your objects:假设 result.data 是您的对象数组:

The reason you're getting undefined because name doesn't exist on your result (as the object keys are actually called "Name1", "Name2", etc).您之所以未定义,是因为结果中不存在name (因为 object 键实际上称为“Name1”、“Name2”等)。

If possible (and perhaps out of scope of the question), I'd change the result.data to return in this format:如果可能的话(也许在问题的 scope 之外),我会改变 result.data 以这种格式返回:

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

Then you could simply do a map over the results like so:然后您可以简单地对结果执行 map,如下所示:

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

If you are unable to change the structure of the results, my suggestion would be to convert the data to an object as per this answer .如果您无法更改结果的结构,我的建议是按照此答案将数据转换为 object 。

Firstly convert the data into array of objects.首先将数据转换为对象数组。

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

And then you would be able to fetch the key from this array然后你就可以从这个数组中获取密钥

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

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