0

image below is the console result. Let say I want to print result for department which contain vary answers (example 1A, 2B, 3C). So how to know the length of each inside department? The length of 1A, the length of 2B and so on.

enter image description here

enter image description here

I am trying do like this but error.

var department = response.data[0].department['1A'].length;
console.log(department)
$("#department").text(department);

var department2 = response.data[0].department['1B'].length;
console.log(department2)
$("#department2").text(department2);

JSON

{
  "status": "Success",
  "data": [
    {
      "user_id": 3,
      "address_line1": null,
      "address_line2": null,
      "country": null,
      "date_of_birth": null,
      "department": "1A",
      "district": null,
      "gender": "Male",
      "workgroup": "AM",
      "name": "Michael"
    },
    {
      "user_id": 4,
      "address_line1": null,
      "address_line2": null,
      "country": null,
      "date_of_birth": null,
      "department": "2B",
      "district": null,
      "gender": "Male",
      "workgroup": "MGR",
      "name": "Mike"
    },
    {
      "user_id": 5,
      "address_line1": null,
      "address_line2": null,
      "country": null,
      "date_of_birth": null,
      "department": "3C",
      "district": null,
      "gender": "Female",
      "workgroup": "AGM",
      "name": "Rachel"
    },
    {
      "user_id": 6,
      "address_line1": null,
      "address_line2": null,
      "country": null,
      "date_of_birth": null,
      "department": "3C",
      "district": null,
      "gender": "Male",
      "workgroup": "AGM",
      "name": "Joe"
    }
  ]
}
10
  • 1
    How does that path response.data[0].user_privilege['1A'].length; match response.data[0].department? Commented Jul 27, 2020 at 13:38
  • 1
    Please include a more complete sample of your data as text, not as an image of text. You can use console.log(JSON.stringify(obj, null, 2)). Commented Jul 27, 2020 at 13:39
  • sorry I have correct my posting. Commented Jul 27, 2020 at 13:40
  • Add the complete JSON response please. Commented Jul 27, 2020 at 13:46
  • I have update the json. it is sufficient? Commented Jul 27, 2020 at 13:54

2 Answers 2

2

You can simply use filter function on your response.data to count the specific strings you have it your JSON data using .length

Run snippet below to see the counts for each department.

var response = {
  "status": "Success",
  "data": [{
      "user_id": 3,
      "address_line1": null,
      "address_line2": null,
      "country": null,
      "date_of_birth": null,
      "department": "1A",
      "district": null,
      "gender": "Male",
      "workgroup": "AM",
      "name": "Michael"
    },
    {
      "user_id": 4,
      "address_line1": null,
      "address_line2": null,
      "country": null,
      "date_of_birth": null,
      "department": "2B",
      "district": null,
      "gender": "Male",
      "workgroup": "MGR",
      "name": "Mike"
    },
    {
      "user_id": 5,
      "address_line1": null,
      "address_line2": null,
      "country": null,
      "date_of_birth": null,
      "department": "3C",
      "district": null,
      "gender": "Female",
      "workgroup": "AGM",
      "name": "Rachel"
    },
    {
      "user_id": 6,
      "address_line1": null,
      "address_line2": null,
      "country": null,
      "date_of_birth": null,
      "department": "3C",
      "district": null,
      "gender": "Male",
      "workgroup": "AGM",
      "name": "Joe"
    }
  ]
}

let count1 = response.data.filter(x => x.department == '1A').length;
let count2 = response.data.filter(x => x.department == '2B').length;
let count3 = response.data.filter(x => x.department == '3C').length;

console.log(count1)
console.log(count2)
console.log(count3)

Sign up to request clarification or add additional context in comments.

Comments

0

Looking at your json, within your data you have a list of objects, and you're trying to find the length of one of the strings (department) inside this object. The way you've worded the question is a little confusing, but I think this is what you are aiming to do:

var department = response.data[0].department.length;
console.log(department)
$("#department").text(department);

According to the JSON you've provided, department is not an array, if it was an array then the JSON would look like this for each object within data:

    {
      "user_id": 3,
      "address_line1": null,
      "address_line2": null,
      "country": null,
      "date_of_birth": null,
      "department": [ "1A", "2A", "3A" ],
      "district": null,
      "gender": "Male",
      "workgroup": "AM",
      "name": "Michael"
    },

I also think you're misunderstanding what length does? length returns the number of characters in the string, meanwhile the image you have implies that you are trying to get the number for the department out of the value that department is set to. If the format of each department is consistent, then you can simply do a substring to get just the first character of the department like this:

var departmentNumber = response.data[0].department.substring(0, 0);

or with charAt() like this:

var departmentNumber = response.data[0].department.charAt(0);

This would only take the first character from the string, meaning you get the department number that you are potentially after.

I would recommend, if you can, that you provide a different variable in addition to department - something like departmentNum - to save the need to perform a selection from a string like this.

1 Comment

Hi, I replied from Satpal answers above. something like that.

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.