2

I am trying loop through a JSON object to extract data from it. When I run my code I get the error:

Type Error: element.listing.forEach is not a function.

I would also like to note that I have done this earlier in my code and it works perfectly. I have tried the solutions posted on forEach is not a function error with JavaScript array. None of them worked. THe string was just filled with white spaces. Here is my code:

var JSONBody = JSON.parse(body);
var dataString = "";
var count = 0;

//Get required data from response body:
JSONBody.forEach(function(element) {
if(element.listing != null){
    element.listing.forEach(function (element2) {
        dataString = dataString + element2.media[0].url;
        dataString = dataString + "!!!";
        dataString = dataString + element2.propertyDetails.bedrooms;
        dataString = dataString + "!!!";
        dataString = dataString + element2.propertyDetails.bathrooms;
        dataString = dataString + "!!!";
        dataString = dataString + element2.propertyDetails.carspaces;
        dataString = dataString + "!!!";
        dataString = dataString + element2.propertyDetails.displayableAddress;
        dataString = dataString + "|||";

        //console.log(element2.media[0].url);
        count++;
    }, this);
    console.log(dataString);
    }else{
        dataString = "";
        console.log("Listings is NULL!");
    }
}, this);

JSONBody looks like:

[ { type: 'PropertyListing',
    listing:
     { listingType: 'Rent',
       id: 11658083,
       advertiser: [Object],
       priceDetails: [Object],
       media: [Array],
       propertyDetails: [Object],
       headline: 'Ultra Modern Furnished Apartment with Stunning Views',
       summaryDescription: '<b>Ultra Modern Furnished Apartment with Stunning Views</b><br />Brisbane\'s newest luxury complex \'Spire\' hits the market with huge popularity. Apartment 2704 offers the lucky resident the very best of the already stunning facilities and views with 27t...',
       hasFloorplan: false,
       hasVideo: false,
       labels: [Array],
       inspectionSchedule: [Object],
       listingSlug: '2704-550-queen-street-brisbane-city-qld-4000-11658083' } },
  { type: 'PropertyListing',
    listing:
     { listingType: 'Rent',
       id: 8980993,
       advertiser: [Object],
       priceDetails: [Object],
       media: [Array],
       propertyDetails: [Object],
       headline: 'High Floor with views in Parkland Paradise & Pet friendly\n',
       summaryDescription: '<b>High Floor with views in Parkland Paradise & Pet friendly\n</b><br />Lap up luxury living with the world’s largest inner-city sub-tropical parklands at your doorstep!! \r\n\r\n\r\n*\t3 good size bedrooms\r\n*\t2 bath rooms\r\n*\t1 car space\r\n*\t3 phone lines\r\n*\tFu...',
       hasFloorplan: true,
       hasVideo: false,
       labels: [],
       inspectionSchedule: [Object],
       listingSlug: '7054-7-parkland-blvd-brisbane-city-qld-4000-8980993' } },
  { type: 'PropertyListing',
    listing:
     { listingType: 'Rent',
       id: 11626727,
       advertiser: [Object],
       priceDetails: [Object],
       media: [Array],
       propertyDetails: [Object],
       headline: 'STUNNING INNER CITY PARKLAND APARTMENT!',
       summaryDescription: '<b>STUNNING INNER CITY PARKLAND APARTMENT!</b><br />A gorgeous inner city apartment capturing stunning parkland views, this two bedroom executive apartment offers it\'s new tenant a fantastic inner city lifestyle!\r\nComfortable, local and convenient, per...',
       hasFloorplan: false,
       hasVideo: false,
       labels: [],
       inspectionSchedule: [Object],
       listingSlug: '3113-3-parkland-boulevard-brisbane-city-qld-4000-11626727' } },
  { type: 'PropertyListing',
    listing:
     { listingType: 'Rent',
       id: 11660275,
       advertiser: [Object],
       priceDetails: [Object],
       media: [Array],
       propertyDetails: [Object],
       headline: 'ENJOY EXPANSIVE, UNINTERRUPTED VIEWS OVER THE BOTANIC GARDENS AND BRISBANE RIVER at ABIAN..!!',
       summaryDescription: 'ENJOY EXPANSIVE, UNINTERRUPTED VIEWS OVER THE BOTANIC GARDENS AND BRISBANE RIVER at ABIAN..!!<br />Presenting an exclusive opportunity to lease this rare three bedroom luxury apartment. For executive renters wanting to secure a large, sophistica...',
       hasFloorplan: false,
       hasVideo: false,
       labels: [Array],
       inspectionSchedule: [Object],
       listingSlug: '2702---140-alice-street-brisbane-city-qld-4000-11660275' } },
  { type: 'PropertyListing',
    listing:
     { listingType: 'Rent',
       id: 11660130,
       advertiser: [Object],
       priceDetails: [Object],
       media: [Array],
       propertyDetails: [Object],
       headline: 'ABIAN - unprecedented luxurious lifestyle!!! LOCATION LOCATION LOCATION..',
       summaryDescription: '<b>ABIAN - unprecedented luxurious lifestyle!!! LOCATION LOCATION LOCATION..</b><br />A SCULPTURAL RIVERFRONT MASTERPIECE BY SUNLAND GROUP - this building has to be seen to be believed.\r\n\r\nThis HUGE 160SQM 3 bedroom apartment on the 23rd floor offers o...',
       hasFloorplan: false,
       hasVideo: false,
       labels: [Array],
       inspectionSchedule: [Object],
       listingSlug: '2302---140-alice-street-brisbane-city-qld-4000-11660130' } },
  { type: 'PropertyListing',
    listing:
     { listingType: 'Rent',
       id: 11660012,
       advertiser: [Object],
       priceDetails: [Object],
       media: [Array],
       propertyDetails: [Object],
       headline: 'ABIAN BOTANIC GARDENS -BRISBANE\'S NEWEST BUILDING OFFERING WORLD-CLASS FACILITIES..!',
       summaryDescription: 'ABIAN BOTANIC GARDENS -BRISBANE\'S NEWEST BUILDING OFFERING WORLD-CLASS FACILITIES..!<br />Located in the most sought after residential quarter of the CBD, you will have immediate access to the Botanical Gardens, South Bank as well as the abundan...',
       hasFloorplan: false,
       hasVideo: false,
       labels: [Array],
       inspectionSchedule: [Object],
       listingSlug: '1601---140-alice-street-brisbane-city-qld-4000-11660012' } },
  { type: 'PropertyListing',
    listing:
     { listingType: 'Rent',
       id: 11658812,
       advertiser: [Object],
       priceDetails: [Object],
       media: [Array],
       propertyDetails: [Object],
       headline: 'STUNNING 2 BED AT ABIAN BOTANIC GARDENS, $675 P/W..!',
       summaryDescription: '<b>STUNNING 2 BED AT ABIAN BOTANIC GARDENS, $675 P/W..!</b><br />This large two-bedroom apartment embraces sweeping views over the Botanical Gardens, Brisbane River, Kangaroo Point cliffs and South Bank.\r\n\r\nThe bedrooms are generous in proportion and e...',
       hasFloorplan: true,
       hasVideo: false,
       labels: [Array],
       inspectionSchedule: [Object],
       listingSlug: '1301---140-alice-street-brisbane-city-qld-4000-11658812' } },
  { type: 'PropertyListing',
    listing:
     { listingType: 'Rent',
       id: 11629051,
       advertiser: [Object],
       priceDetails: [Object],
       media: [Array],
       propertyDetails: [Object],
       headline: 'ABIAN  Brisbanes new standard in luxury living!!',
       summaryDescription: '<b>ABIAN  Brisbanes new standard in luxury living!!</b><br />Enjoy uninterrupted views of the River City skyline, Botanical Gardens, Kangaroo Point Cliffs and South Brisbane. Positioned at the unrivalled location directly opposite the Botanic Gardens a...',
       hasFloorplan: false,
       hasVideo: false,
       labels: [],
       inspectionSchedule: [Object],
       listingSlug: '3103-140-alice-street-brisbane-city-qld-4000-11629051' } },
  { type: 'PropertyListing',
    listing:
     { listingType: 'Rent',
       id: 11578249,
       advertiser: [Object],
       priceDetails: [Object],
       media: [Array],
       propertyDetails: [Object],
       headline: 'SWEEPING RIVER VIEWS- EXQUISITE APARTMENT LIVING! 3 BEDROOM APARTMENT - MERITON!',
       summaryDescription: '<b>SWEEPING RIVER VIEWS- EXQUISITE APARTMENT LIVING! 3 BEDROOM APARTMENT - MERITON!</b><br />Call Daleen on 0427 689 598 to arrange your inspection today!\r\n\r\nCentral CBD Location | River + City Views | Close to transport | Onsite Building Manager | Gym...',
       hasFloorplan: true,
       hasVideo: false,
       labels: [],
       inspectionSchedule: [Object],
       listingSlug: '501-adelaide-street-brisbane-city-qld-4000-11578249' } },
  { type: 'PropertyListing',
    listing:
     { listingType: 'Rent',
       id: 11657497,
       advertiser: [Object],
       priceDetails: [Object],
       media: [Array],
       propertyDetails: [Object],
       headline: 'AMAZING NEW WATER-VIEW APARTMENT AT ABIAN BOTANIC GARDEN IS NOW READY FOR RENT',
       summaryDescription: '<b>AMAZING NEW WATER-VIEW APARTMENT AT ABIAN BOTANIC GARDEN IS NOW READY FOR RENT</b><br />This spectacular brand-new home on 16th floor features 2 bedrooms, 2 bathrooms, master bedroom with walk in robe and ensuite, air conditioning, single lockup gar...',
       hasFloorplan: false,
       hasVideo: false,
       labels: [],
       inspectionSchedule: [Object],
       listingSlug: '--140-alice-street-brisbane-city-qld-4000-11657497' } } ]
5
  • 3
    Show us JSONBody in the console Commented Sep 21, 2017 at 2:08
  • whats body,how does it look ? like an array ` [ 1,2,3 ] ` or an object ` { 1:'potato', 2: 'orange' } ` Commented Sep 21, 2017 at 2:08
  • Body is a response from a server that was initially a string in the format of a JSON string Commented Sep 21, 2017 at 2:10
  • Well that does not help at all.... We need to know what the actual structure is. Commented Sep 21, 2017 at 2:12
  • And now we see you have an Object and an object does not have forEach. There is no reason to loop over it just reference it. EG: reference listingType like this: JSONBody.forEach(function(element) { console.log(element.listing. listingType) }) Commented Sep 21, 2017 at 2:14

1 Answer 1

11

JSONBody is an Object, not Array. So, you can iterate through it using Object.keys (returns an array of keys) and Array.prototype.forEach (you can pass 3 parameters to callback function for more control: item, index, array):

Object.keys(JSONBody).forEach(function(key, index) {
    // key: the name of the object key
    // index: the ordinal position of the key within the object 
});
Sign up to request clarification or add additional context in comments.

1 Comment

Object.keys(...) was the relevant information for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.