无法从具有Node js的js数组中获取数据

时间:2017-03-15 19:59:40

标签: arrays node.js javascript-objects

我有一个像这样的js对象数组。

[  
   {  
      name:"Japanilainen ravintola Koto",
      rating:3.9,
      photo:[  
         {  
            height:2160,
            html_attributions:[  
               "<a href="               https://maps.google.com/maps/contrib/110335983983267481647/photos">Hannes Junnila</a>"
            ],
            photo_reference:"CoQBdwAAAMDlivT0nOnYg8jC1txZ3RbfBR59XvKN0WphDbRVUXaUTQclzzaIaXJ8-p7s3x_aG67AUsM_HLNML6pzGl3v_wV2D-eudH_3wy2cB1ROrRgGcGyf4lRuNpE3WwXYbYZu6EK8oEPiJ5B17Lybj-eVbYM2EgVVBgOrUJhsblY1mfxWEhAZ4oHCFakH-hgkbksfGa2uGhQe4aUeOrS2isAir01KUwQ7N3Ce2Q",
            width:2269
         }
      ]
   },
   {  
      name:"Kin Sushi Helsinki",
      rating:4.2,
      photo:[  
         {  
            height:2988,
            html_attributions:[  
               "<a href="               https://maps.google.com/maps/contrib/103078599092779880596/photos">Stephan Winter</a>"
            ],
            photo_reference:"CoQBdwAAAN4iMumSbQjtRnJIH1AKRdbSfnI02WGh11r1xaVnZl1ohebKp6zpAS4mmJFqTagrIqUJ39kzulVI0sz2UzzfaVdsAFc5f80PnOCzSLqL5gnpsqv90dVJIqUWD3Bcc9TgYPPs3oGwyekkOsmjQ59o9yqdoF5GzrpaKkojhMNLxpfzEhBKpRkA2CzINpUzAAe3e90TGhQ_KbYCmtJYLfVGIu1kZkzQIAwE4A",
            width:5312
         }
      ]
   }
]

我为每个人做了这个数组。

response.results.forEach((entry)=>{
         var restaurantName = {
          "name" : entry.name,
          "rating" : entry.rating,
          "photo_reference" : entry.photos
        }
        arr.push(restaurantName);
      });
        res.send(arr);

然后我将数组发送到我的浏览器,以便我可以看到它。 我要做的是从photo_reference获取entry.photos 我尝试了entry.photos[0].photo_reference以及更多方法,而且在所有这些方法中,我都无法正常阅读,现在我不知道如何获取这些信息。

2 个答案:

答案 0 :(得分:1)

我编辑了一些变量名称,以便在此处进行模拟,但只需将map数组中的对象photo添加到其引用中,您就可以得到一组照片引用。

&#13;
&#13;
const data = [  
   {  
      name:"Japanilainen ravintola Koto",
      rating:3.9,
      photo:[  
         {  
            height:2160,
            html_attributions:[  
               '<a href="https://maps.google.com/maps/contrib/110335983983267481647/photos">Hannes Junnila</a>'
            ],
            photo_reference:"CoQBdwAAAMDlivT0nOnYg8jC1txZ3RbfBR59XvKN0WphDbRVUXaUTQclzzaIaXJ8-p7s3x_aG67AUsM_HLNML6pzGl3v_wV2D-eudH_3wy2cB1ROrRgGcGyf4lRuNpE3WwXYbYZu6EK8oEPiJ5B17Lybj-eVbYM2EgVVBgOrUJhsblY1mfxWEhAZ4oHCFakH-hgkbksfGa2uGhQe4aUeOrS2isAir01KUwQ7N3Ce2Q",
            width:2269
         }
      ]
   },
   {  
      name:"Kin Sushi Helsinki",
      rating:4.2,
      photo:[  
         {  
            height:2988,
            html_attributions:[  
               '<a href="https://maps.google.com/maps/contrib/103078599092779880596/photos">Stephan Winter</a>'
            ],
            photo_reference:"CoQBdwAAAN4iMumSbQjtRnJIH1AKRdbSfnI02WGh11r1xaVnZl1ohebKp6zpAS4mmJFqTagrIqUJ39kzulVI0sz2UzzfaVdsAFc5f80PnOCzSLqL5gnpsqv90dVJIqUWD3Bcc9TgYPPs3oGwyekkOsmjQ59o9yqdoF5GzrpaKkojhMNLxpfzEhBKpRkA2CzINpUzAAe3e90TGhQ_KbYCmtJYLfVGIu1kZkzQIAwE4A",
            width:5312
         }
      ]
   }
]

const arr = []
data.forEach((entry)=>{
  var restaurantName = {
    "name" : entry.name,
    "rating" : entry.rating,
    "photo_reference" : entry.photo.map(x => x.photo_reference)
  }

  arr.push(restaurantName);
});

console.log(arr);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您的entry.photoes对象数组中未定义

response.results ..您是不是要将其作为entry.photo(在您的foreach函数中)进行访问?