Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:16:55+00:00 2026-05-31T13:16:55+00:00

Json String: (response) {19:{id:19,name:Product,sku:123,price:59.50,cost:25.00},20:{id:20,name:Test,sku:456,price:50.00,cost:40.00}} JavaScript Code: var json = $.parseJSON(response); var items = new

  • 0

Json String: (response)

{"19":{"id":"19","name":"Product","sku":123,"price":"59.50","cost":"25.00"},"20":{"id":"20","name":"Test","sku":456,"price":"50.00","cost":"40.00"}}

JavaScript Code:

  var json = $.parseJSON(response);
  var items = new Array();

  $.each(json, function(index, value){
    items[index] = value.id;
  });

  console.log(items);
  // returns  [ , , , , , , , , , , , , , , , , , , , 19, 20 ]    

I have a JSON object that’s originally built from a php array and converted using json_encode. When I loop through the object – to build a new array based off some of the values, it has null as all the first items, then the last two has the the actual id.

Any ideas what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:16:55+00:00Added an answer on May 31, 2026 at 1:16 pm

    This is because you are using an array for items. When you set items[19], it needs to set values 0-18 first (these are the “null” values). This is because JavaScript doesn’t have associative arrays, only numeric.

    Try using an object for items instead.

    var items = {};
    
    $.each(json, function(index, value){
      items[index] = value.id;
    });
    
    console.log(items); // {19: 19, 20: 20}
    

    P.S. The “null” items in the array are actually undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this JSON response string: {d:{\ID_usuario\:\000130\,\Nombre\:null,\Vipxlo\:0,\Provmun\:null,\Descuentos\:null,\Listaviplocal\:null}}` With this code: - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
I have the following json string: {response:[[{uid:123456,name:LA_}],[{cid:1,name:Something}],[{cid:1,name:Something-else}]]} How can I get Something value? I
I am getting JSON response from the server..Inside that JSON string i am having
I'm loading a JSON string in Django using simplejson , thus: obj = json.loads('{name:
Take the following JSON string (generated by some ExtJS code - but that's irrelevant):
Im trying to parse an JSON response string to my class objects.. I can't
i use Newtonsoft.Json.Net20.dll Html <div id=a runat=server> </div> Code string kb = a; string
I have the following JSON object as a String: [{Add1:101,Description:null,ID:1,Name:Bundesverfassung,Short:BV},{Add1:220,Description:null,ID:2,Name:Obligationenrecht,Short:OR},{Add1:210,Description:null,ID:3,Name:Schweizerisches Zivilgesetzbuch,Short:ZGB},{Add1:311_0,Description:null,ID:4,Name:Schweizerisches Strafgesetzbuch,Short:null}] Now i
Hi I have this snippet of code : protected void onPostExecute(String response) { String
Below is my block of code where I am retrieving one json string through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.