-2

When I type Monday and click Search I want to get all of Monday's JSON data and not just the first row. I've tried modifying the forEach function and also using two ng-repeats in html. So I guess it's the JSON Data structure? Fiddle.

         "Monday": [{
            "Name": "John",
                "Address": "Street",
                "Phone": "111",
                "Status": "Sleep"
        },
        {
            "Name": "Sam",
                "Address": "Street2",
                "Phone": "2",
                "Status": "Awake"
        }
        ],

         "Tuesday": [{..............................

1 Answer 1

2

You need to use this code:

angular.forEach($scope.items, function (value, key) {
            if (key === enteredValue) {
                angular.forEach(value, function(item) {
                    $scope.results.push({
                        name: key,
                        address: item.Address,
                        phone: item.Phone, 
                        status: item.Status
                    });
                });

            }
        });

The problem was that you only used one forEach, when you actually need two. Not a problem with your data structure.

You could also just do this, however:

    angular.forEach($scope.items[enteredValue], function (item, key) {
           $scope.results.push({
                    name: enteredValue,
                    address: item.Address,
                    phone: item.Phone, 
                    status: item.Status
                });

    });

which would be must simpler. I hope this helps!

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

1 Comment

Thank you. Fiddle .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.