-1

I have a data object like this :

$scope.data =  [
      {
        "name": "1001",
        "queue": [
          {
            "number": "111",
          }
        ]
      },
      {
        "name": "1002",
        "queue": [

        ]
      },
      {
        "name": "1008",
        "queue": [
          {
            "number": "222",
          }
        ]
      }]

I'm using angular js. I have first declared 3 arrays:

$scope.a = [];
$scope.b = [];
$scope.c = [];

The desired output that I want :

If I do console.log($scope.a); then the output should be :

{
    "name": "1001",
    "queue": [
      {
        "number": "111",
      }
    ]
  }

If I do console.log($scope.b); then the output should be :

{
    "name": "1008",
    "queue": [
      {
        "number": "222",
      }
    ]
  }

If I do console.log($scope.c); then the output should be :

{
    "name": "1002",
    "queue": [

    ]
  }

I want to go through the data and push the objects where the queue number is 111 into $scope.a, push the objects where the queue number is 222 into $scope.b and push the objects where the queue array is empty into $scope.c. I'm not able to figure how to filter this object by checking the values from the queue array. How do I do it in AngularJS?

8
  • Please update the question with output. Commented Nov 15, 2018 at 7:45
  • There's no output. I want to filter the data based on the queue number and store it into the different arrays accordingly Commented Nov 15, 2018 at 7:59
  • Please add the sample output for the above input. Commented Nov 15, 2018 at 8:01
  • As per the JSON in your question, can you show what is needed inside $scope.a, $scope.b and $scope.c? Commented Nov 15, 2018 at 8:05
  • check the updated question for the desired output Commented Nov 15, 2018 at 8:10

2 Answers 2

2

It is quite simple to solve this problem @Navin. Just loop through the array of objects checking the queue number within each object. As and when you encounter a right match, insert into the corresponding array.

I have attached the working snippet below.

var data = [{
    "name": "1001",
    "queue": [{
      "number": "111",
    }]
  },
  {
    "name": "1002",
    "queue": [

    ]
  },
  {
    "name": "1008",
    "queue": [{
      "number": "222",
    }]
  }
];
var a = [];
var b = [];
var c = [];
for (i = 0; i < data.length; i++) {
  if (data[i].queue.length != 0) {
    for (j = 0; j < data[i].queue.length; j++) {
      if (data[i].queue[j].number == 111) {

        a.push(data[i]);
      } else if (data[i].queue[j].number == 222) {
        b.push(data[i]);
      }
    }
  } else {
    c.push(data[i]);
  }
}
a = JSON.stringify(a);
b = JSON.stringify(b);
c = JSON.stringify(c);
console.log(a + "\n" + b + "\n" + c);

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

1 Comment

can you help me with this
0

do it through a for loop , as $scope.data is an object array for eg.

for(i=0;i<=$scope.data.length ;i++){
   if($scope.data[i].queue.length != 0){
       for(j=0;j<=$scope.data[i].queue.length;j++){
         if($scope.data[i].queue[j].number == 111){
               $scope.a.push =$scope.data[i]
           }else if($scope.data[i].queue[j].number == 222){
                      $scope.b.push =$scope.data[i]    
                   }
        }
    }else{
             $scope.c.push = $scope.data[i]
           }
 }

I hope this may help you.

1 Comment

Hey Anny, you've got the logic correct here in your answer. But there's a mistake in how you push the data into the array. The correct way to push is by placing the part that is to be pushed inside parenthesis (). Check my answer. Your logic is right though.

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.