简体   繁体   English

访问名称相同的javascript对象中的数据

[英]Accessing data in a javascript object that has the same name

I have a javascript object that looks similar to this: 我有一个看起来像这样的javascript对象:

var data = {
  Message: {
       sent: 'complete',
       received: true,
       time: '4:06 PM' 
   },
  Message: {
       sent: 'complete',
       received: false,
       time: '9:01 AM' 
   },
  Message {
     ...... // and so on
   }    
}

And I want to access each one of the sent , received and time objects and push them to an array.So for example, it might look like this when its done: 我想访问的每一个sentreceivedtime的对象和他们推到一个array.So例如,它可能看起来像这样它的完成时:

['complete', 'complete', 'Not complete'] // sent
[true, true, false]  // received 
['4:06 PM','9:01 AM', '2:00 PM' ] // time

I know this is pretty basic, but I'm not sure how to get every one of sent , received and time , only the last one (relatively new to js). 我知道这是非常基本的,但是我不确定如何获取sentreceivedtime每一个,仅获取最后一个(对于js而言相对较新)。

How can I get every one of sent , received and time ? 如何获得sentreceivedtime每一个? I'm pretty sure I could figure out the array portion, however if you include it in the solution I'd really appreciate it! 我很确定我可以弄清楚数组部分,但是如果您将其包括在解决方案中,我将非常感激!

You can not have two keys in an object with the same name. 同一名称的对象中不能有两个键。 Instead try an array: 而是尝试一个数组:

 var messages = [ { sent: 'complete', received: true, time: '4:06 PM' }, { sent: 'complete', received: false, time: '9:01 AM' }, { sent: 'complete', received: false, time: '1:01 PM' } ]; messages.forEach( (message) => { console.log(message.sent, message.time); } ); 

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

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