Chirag S Modi Chirag S Modi - 6 months ago 20
Node.js Question

How can i get data from array in express

I am using rest API with express ejs in nodejs.
from that api i get JSON data like below.
In this data there is an array inside array.
Issue is I want value of feature_image value of guid. but when i try to get that value it show me

has no method 'forEach
error.

[
{
"ID": 161,
"title": "Compant Name 3",
"status": "publish",
"type": "testimonial",
"author": {
"ID": 1,
"username": "admin",
"name": "admin",
"first_name": "",
"last_name": "",
"nickname": "admin",
"slug": "admin",
"URL": "",
"avatar": "http://2.gravatar.com/avatar/56282b1eb6c5d293a03d2c167924b197?s=96",
"description": "",
"registered": "2015-04-28T10:14:02+00:00",
"meta": {
"links": {
"self": "http://tryitoneyewear.com/wp-json/users/1",
"archives": "http://tryitoneyewear.com/wp-json/users/1/posts"
}
}
},
"content": "<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem </p>\n",
"parent": null,
"link": "http://tryitoneyewear.com/testimonial/compant-name-3/",
"date": "2015-06-04T07:33:29",
"modified": "2015-06-04T08:01:26",
"format": "standard",
"slug": "compant-name-3",
"guid": "http://tryitoneyewear.com/?post_type=testimonial&#038;p=161",
"excerpt": "<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem</p>\n",
"menu_order": 0,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"date_tz": "UTC",
"date_gmt": "2015-06-04T07:33:29",
"modified_tz": "UTC",
"modified_gmt": "2015-06-04T08:01:26",
"meta": {
"links": {
"self": "http://tryitoneyewear.com/wp-json/posts/161",
"author": "http://tryitoneyewear.com/wp-json/users/1",
"collection": "http://tryitoneyewear.com/wp-json/posts",
"replies": "http://tryitoneyewear.com/wp-json/posts/161/comments",
"version-history": "http://tryitoneyewear.com/wp-json/posts/161/revisions"
}
},
"featured_image": {
"ID": 28,
"title": "dummy_img",
"status": "inherit",
"type": "attachment",
"author": {
"ID": 1,
"username": "admin",
"name": "admin",
"first_name": "",
"last_name": "",
"nickname": "admin",
"slug": "admin",
"URL": "",
"avatar": "http://2.gravatar.com/avatar/56282b1eb6c5d293a03d2c167924b197?s=96",
"description": "",
"registered": "2015-04-28T10:14:02+00:00",
"meta": {
"links": {
"self": "http://tryitoneyewear.com/wp-json/users/1",
"archives": "http://tryitoneyewear.com/wp-json/users/1/posts"
}
}
},
"content": "<p class=\"attachment\"><a href='http://tryitoneyewear.com/wp-content/uploads/2015/04/dummy_img.jpg'><img width=\"200\" height=\"200\" src=\"http://tryitoneyewear.com/wp-content/uploads/2015/04/dummy_img.jpg\" class=\"attachment-medium\" alt=\"dummy_img\" /></a></p>\n",
"parent": null,
"link": "http://tryitoneyewear.com/?attachment_id=28",
"date": "2015-04-29T06:50:48",
"modified": "2015-04-29T06:50:48",
"format": "standard",
"slug": "dummy_img",
"guid": "http://tryitoneyewear.com//wp-content/uploads/2015/04/dummy_img.jpg",
"excerpt": null,
"menu_order": 0,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"date_tz": "UTC",
"date_gmt": "2015-04-29T06:50:48",
"modified_tz": "UTC",
"modified_gmt": "2015-04-29T06:50:48",
"meta": {
"links": {
"self": "http://tryitoneyewear.com/wp-json/media/28",
"author": "http://tryitoneyewear.com/wp-json/users/1",
"collection": "http://tryitoneyewear.com/wp-json/media",
"replies": "http://tryitoneyewear.com/wp-json/media/28/comments",
"version-history": "http://tryitoneyewear.com/wp-json/media/28/revisions"
}
},
"terms": [],
"source": "http://tryitoneyewear.com/wp-content/uploads/2015/04/dummy_img.jpg",
"is_image": true,
"attachment_meta": {
"width": 200,
"height": 200,
"file": "2015/04/dummy_img.jpg",
"sizes": {
"thumbnail": {
"file": "dummy_img-150x150.jpg",
"width": 150,
"height": 150,
"mime-type": "image/jpeg",
"url": "http://tryitoneyewear.com/wp-content/uploads/2015/04/dummy_img-150x150.jpg"
},
"owl_widget": {
"file": "dummy_img-180x100.jpg",
"width": 180,
"height": 100,
"mime-type": "image/jpeg",
"url": "http://tryitoneyewear.com/wp-content/uploads/2015/04/dummy_img-180x100.jpg"
}
},
"image_meta": {
"aperture": 0,
"credit": "",
"camera": "",
"caption": "",
"created_timestamp": 0,
"copyright": "",
"focal_length": 0,
"iso": 0,
"shutter_speed": 0,
"title": "",
"orientation": 0
}
}
},
"terms": []
}
]

Answer

You should learn a bit more about json and probably javascript

Your json is array with single object in it. The featured_image is also object so to get the value you need something like

your_json[0].featured_image.guid