How do I loop through my arrays using .each() without duplicating data?
I'm using the $.get method to return some JSON that is an object in a bunch of arrays.
I want to walk through the arrays and spit out the keys and values.
My JSON object has a bunch of city names as keys of arrays, then has quotes within those arrays belonging to each city.
Here's the result I'm getting back from my $.get method (after parsing to JSON object);
-
- Aberdeen: Array[10]
- 0: "I'm about to explode Chinese and chocolates such a combination and my fellow workmates were a right laugh!"
- 1: "�@TylerProsper: @_BoricuaMorena timeout�??????"
- 2: "@ashleighca haha why not? I wouldn't want him as much good looking as he is, stay away from my asshole"
- 3: "Shite weather and ridiculously tired, time for a sleep and hope my coupon is alive when I wake up!"
- 4: "what was the score boys and any use getting #mwi @calummcbain @kieranlynch95 @sobykerr72 @DaleSimeon88 @kieranferns @Dylnamitee"
- 5: "@kirstyfisherxx im here babe "
- 6: "@StrawberrycurIs last min Xmas pressie I've got stock, Chelmsford peeps, anytime between now and Xmas eve, collection only"
- 7: "@rhimarkable i'm a left tit you're a right tit hahah night"
- 8: "i am really bored."
- 9: "@xrebeccamallisx im home tomorrow sweetcheeks! who's house first on Xmas day? Hahahaha"
- length: 10
- __proto__: Array[0]
- Armagh: Array[4]
- Bath: Array[5]
- Belfast: Array[10]
- Birmingham: Array[10]
- Bradford: Array[10]
- Brighton: Array[1]
- Bristol: Array[10]
- Cambridge: Array[5]
- Canterbury: Array[5]
- Cardiff: Array[10]
- Chester: Array[7]
- Chichester: Array[6]
- Coventry: Array[10]
- Derby: Array[10]
- Dundee: Array[10]
- Durham: Array[5]
- Edinburgh: Array[10]
- Glasgow: Array[10]
- Gloucester: Array[10]
- Hereford: Array[5]
- Inverness: Array[9]
- Lancaster: Array[9]
- Leeds: Array[10]
- Leicester: Array[10]
- Lichfield: Array[4]
- Lisburn: Array[4]
- Liverpool: Array[10]
- Manchester: Array[10]
- Newport: Array[10]
- Newry: Array[2]
- Nottingham: Array[10]
- Oxford: Array[8]
- Peterborough: Array[8]
- Plymouth: Array[10]
- Portsmouth: Array[10]
- Preston: Array[4]
- Salford: Array[10]
- Sheffield: Array[10]
- Southampton: Array[10]
- St Albans: Array[10]
- Stirling: Array[5]
- Sunderland: Array[6]
- Swansea: Array[10]
- Wakefield: Array[10]
- Winchester: Array[6]
- Wolverhampton: Array[10]
- Worcester: Array[8]
- York: Array[5]
- __proto__: Object
I want to spit out the array keys (the city names) and then each array value (quotes) in this structure;
- <div>
- <h4>Aberdeen</h4>
- <ul>
- <li> <q>"I'm about to explode Chinese and chocolates such a combination and my fellow workmates were a right laugh!"</q> </li>
- <li> <q>"�@TylerProsper: @_BoricuaMorena timeout�??????"</q>
- </li>
- <!-- ... etcetera -->
- </ul>
- </div>
- <div>
- <h4>Armagh</h4>
- <ul>
- <!-- ... etcetera -->
- </ul>
- </div>
What I have at the moment is some jQuery that spits out different HTML but the worrying part is it loops through the array values for each array. Sorry, I don't think I explained that well.
Instead of just outputting the quotes for each city, it outputs every quote for all the cities in each city.
Here's my jQuery code;
- $.get('../../controller/cities.php', function(result) {
- var citiesTweets = jQuery.parseJSON(result);
- console.log(citiesTweets);
- $('#all_cities').append('<div></div>').append('<ul></ul>');
- $.each( citiesTweets, function( k, v ) {
- $('#all_cities ul').append('<li></li>');
-
- $('#all_cities li').append('<h4>' + k + '</h4>');
-
- $.each(v, function(k,v){
- $('#all_cities li').append('<blockquote>' + this + '</blockquote>');
- });
-
- });
- });
My problem question is:
How do you get jQuery to loop through only the elements in one array, rather than all of the arrays?
Sorry, that's probably the wrong question but I don't know how to phrase it other than, "What's going on here?".