我正在尝试在对象数组中获取特定对象。

var arr = [
           {
             id: "asd"
             description: "V1",
             output: [{type: "A", id:"ge"}]
           },
           {
             id: "qwe"
             description: "V2",
             output: [{type: "B", id:"de"}]
           }
          ];

console.log(arr.description);




console.log(arr.output.type);


所需的输出将是:

 {
    { description: "V1", output: {type: "A", id: "ge"}},
    { description: "V2", output: {type: "B", id: "de"}}
 }


而且我想将对象的'output'数组转换为对象,就像输出一样。如何实现呢?

在这里帮我

最佳答案

首先,您在数组语法中有一个小错误。
最主要的是:

var arr = [
       {
         id: "asd",
         description: "V1",
         output: [{type: "A", id:"ge"}]
       },
       {
         id: "qwe",
         description: "V2",
         output: [{type: "B", id:"de"}]
       }
      ];


如果您只想登录它,可以使用以下命令:

arr.forEach(function(item){console.log(item.description)})


此代码:
对于arr数组中的每个对象,请执行以下操作:
将该对象的名称设置为item
最后在description中登录item

您可以以其他形式使用此代码

祝好运

09-25 17:25
查看更多