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 8356577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:14:01+00:00 2026-06-09T10:14:01+00:00

I have the following JSON: {data:[{id:1,client_id:1},{id:2,client_id:1}]} I’m trying to do a for…in but something

  • 0

I have the following JSON:

{"data":[{"id":1,"client_id":1},{"id":2,"client_id":1}]}

I’m trying to do a for...in but something is not going well.

Look at my code:

for (post in data) {
    console.log(post.client_id); //return undefined
}

But if I do this:

for (i=0 ; i<data.length; i++) {
    console.log(data[i].client_id); //return the correct value
}

Why can’t I iterate with for..in in this case?

  • 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-06-09T10:14:02+00:00Added an answer on June 9, 2026 at 10:14 am

    First off, you should NEVER iterate arrays with for/in. That structure iterates properties of the object. You will get all the array elements, but you may also get other iterable properties of the object too.

    Arrays should always be iterated with a traditional for loop as in:

    for (var i = 0; i < arr.length; i++).

    Second off, you have to make sure you’re following your own data structure properly. You have this:

    var item = {"data":[{"id":1,"client_id":1},{"id":2,"client_id":1}]};
    

    which spread out some more looks like this:

    var item = {
        "data":[
            {"id":1,"client_id":1},
            {"id":2,"client_id":1}
         ]
    };
    

    So, your JSON is an object. That object has one property in it called data and that one property’s value is an array, which contains objects. You would get the first item in the array with this:

    item.data[0]
    

    Or the properties on that object with this:

    item.data[0].id
    item.data[0].client_id
    

    You would iterate all the items in that array like this:

    for (var i = 0; i < item.data.length; i++) {
        // access each object in the array
        item.data[i].id
        item.data[i].client_id
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following JSON data being returned, but for some reason, Javascript or
I have the following code. I'm trying to create the store via json. I
I have a JSON that contains the following: {p1:{debris:{titanium:0,silicum:0}},p2:{debris:{titanium:0,silicum:0}}... I declare the data using
I have the following json code file named: sections.json { section1: { priority: 1,
The following works, if I have something like this: return Json(new { CustomerInfo =
i have the following data as a string in my Action method: string json
I am trying to get JSON data from a string that I have created
I have the following ajax call to webservice to pass json data and get
I have the following json object. var json1 = {00 : 00, 15 :
I have the following JSON String { name:Product, properties: { id: { type:number, description:Product

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.