数组数组的对象数组以字符串

时间:2018-06-11 21:03:52

标签: javascript arrays json

我将JSON对象oAtt声明为

var oAtt = MessageStore.message().attachments();

MessageStore.message()在此处提供的代码中声明:https://github.com/RainLoop/rainloop-webmail/blob/master/dev/Model/Message.js

当我将其输出到控制台oArr时,显示为[object Object],[object Object]。在stringify之后它出现了:

 var oAtt = [
    {"sModelName":"AttachmentModel","disposables":[],"mimeType":"application/pdf","fileName":"sample.pdf","fileNameExt":"pdf","fileType":"pdf","description":null,"estimatedSize":19333,"friendlySize":"19KB","isInline":false,"isLinked":false,"isThumbnail":false,"cid":"","cidWithOutTags":"","contentLocation":"","download":"zyfTZvzczVW6cqlcsIrm3ycmY5soz3DzK5sZhjnSOwaW0wnwGwHtqBvPeo8nVpiWD4yKVIPvAPauJHZufHGRG0Eb4MRmBpkGlvsDGP-gz3CnVp4GorN_xXCI0Gkd2I93IySKMy5iXRt-wML9wxLwTZcrOMLyPp-Kr6b0GlzlHuvk3RqEXRqSlCQTX5piRO6AaRYeMKeq8PSJcId1grMYzFpCnjVSoW_Zj7yTGAsy-D_mzFoCmBdksCweEqPdGhFYFkukblXzaZiTSNGTj1xYOvMIN4XXUZtU0EAPoQ9EFxfPWY_-1Siyge0Gks2zbGs3myxXJg..","folder":"INBOX","uid":"22","mimeIndex":"2","framed":false},
    {"sModelName":"AttachmentModel","disposables":[],"mimeType":"image/jpeg","fileName":"test4.jpg","fileNameExt":"jpg","fileType":"image","description":null,"estimatedSize":26326,"friendlySize":"26KB","isInline":false,"isLinked":false,"isThumbnail":true,"cid":"","cidWithOutTags":"","contentLocation":"","download":"BN0gFmNTTOarHTn7bDrdaLxLQ-HmO36mmT8av0rwGMsO9XeBlK7OWBq4uiR5N3RkgjXBwwDtOcLBTPIx-354pFHsV2BigaNfpvFG7XlkFYyCsOwGQcNS2pR_U53ISNCnDV-bZYNjl6dc-XAPb71qOkkxn-XxWCtfqJl5ngPPQ67owcZUUfIv9ddSsGyX5PhMljxyMq6f_Cz6dp07Xeaex89ED35VTqyJe4C1MyAxq92fJlxBstcj7dM12YbRJzzWeS_B9t6g48mwh-0am6c28uxPAGYc_EP4C3VweITw7Q5uhTtNmycMksTu4CIVepYn","folder":"INBOX","uid":"22","mimeIndex":"3","framed":false}
    ]

当我试图将该对象转换为值数组数组时,它变为

  

sample.pdf,test4.jpg

使用以下代码:

var aAtt = oAtt.map(({fileName}) => [fileName]);

当我直接以oAtt[0].fileName访问它时,我得到了文件名值。此外,当我从主代码(仅声明oAttmap代码行)运行它时,它应该正常工作。

有什么可能是错的?

2 个答案:

答案 0 :(得分:2)

我无法重现你的问题。什么是oArr?您在代码中谈论oAttaAtt



var oAtt = [{
    "sModelName": "AttachmentModel",
    "disposables": [],
    "mimeType": "application/pdf",
    "fileName": "sample.pdf",
    "fileNameExt": "pdf",
    "fileType": "pdf",
    "description": null,
    "estimatedSize": 19333,
    "friendlySize": "19KB",
    "isInline": false,
    "isLinked": false,
    "isThumbnail": false,
    "cid": "",
    "cidWithOutTags": "",
    "contentLocation": "",
    "download": "zyfTZvzczVW6cqlcsIrm3ycmY5soz3DzK5sZhjnSOwaW0wnwGwHtqBvPeo8nVpiWD4yKVIPvAPauJHZufHGRG0Eb4MRmBpkGlvsDGP-gz3CnVp4GorN_xXCI0Gkd2I93IySKMy5iXRt-wML9wxLwTZcrOMLyPp-Kr6b0GlzlHuvk3RqEXRqSlCQTX5piRO6AaRYeMKeq8PSJcId1grMYzFpCnjVSoW_Zj7yTGAsy-D_mzFoCmBdksCweEqPdGhFYFkukblXzaZiTSNGTj1xYOvMIN4XXUZtU0EAPoQ9EFxfPWY_-1Siyge0Gks2zbGs3myxXJg..",
    "folder": "INBOX",
    "uid": "22",
    "mimeIndex": "2",
    "framed": false
  },
  {
    "sModelName": "AttachmentModel",
    "disposables": [],
    "mimeType": "image/jpeg",
    "fileName": "test4.jpg",
    "fileNameExt": "jpg",
    "fileType": "image",
    "description": null,
    "estimatedSize": 26326,
    "friendlySize": "26KB",
    "isInline": false,
    "isLinked": false,
    "isThumbnail": true,
    "cid": "",
    "cidWithOutTags": "",
    "contentLocation": "",
    "download": "BN0gFmNTTOarHTn7bDrdaLxLQ-HmO36mmT8av0rwGMsO9XeBlK7OWBq4uiR5N3RkgjXBwwDtOcLBTPIx-354pFHsV2BigaNfpvFG7XlkFYyCsOwGQcNS2pR_U53ISNCnDV-bZYNjl6dc-XAPb71qOkkxn-XxWCtfqJl5ngPPQ67owcZUUfIv9ddSsGyX5PhMljxyMq6f_Cz6dp07Xeaex89ED35VTqyJe4C1MyAxq92fJlxBstcj7dM12YbRJzzWeS_B9t6g48mwh-0am6c28uxPAGYc_EP4C3VweITw7Q5uhTtNmycMksTu4CIVepYn",
    "folder": "INBOX",
    "uid": "22",
    "mimeIndex": "3",
    "framed": false
  }
];

var aAtt = oAtt.map(({fileName}) => [fileName]);

console.log(oAtt);
console.log(aAtt);




答案 1 :(得分:0)

不知道为什么.map在主代码中使用时无效,但我最终得到了以下代码

var aAtt = new Array();

for(var i=0;i<oAtt.length;i++)
{
   aAtt[i] = new Array();

   aAtt[i].push(oAtt[i].fileName);
   aAtt[i].push(oAtt[i].mimeType);
   aAtt[i].push(oAtt[i].download);
}