0

I have the following data

var data = [
    {
        "h_id": "31",
        "city": "hill",
    },
    {
        "h_id": "13",
        "city": "Bevery Hills",
    },
    {
        "h_id": "5",
        "city": "New York",
    },
    {
        "h_id": "31",
        "city": "New York",
    },
    {
        "h_id": "5",
        "city": "New York",
    }
];

I am getting data in this format

var data1 = [
    {
        "h_id": "31",
        "city": "hill",
    }, {
        "h_id": "13",
        "city": "Bevery Hills",
    }, {
        "h_id": "31",
        "city": "New York",
    }
];

I want data in this format

var data1 = [
    {
        "h_id": "31",
        "city": "hill",
    }, {
        "h_id": "31",
        "city": "Bevery Hills",
    }
];

This is my angularjs script

$scope.data1 = $filter('filter')($scope.data , {h_id:31} )

I want the data to be filter based on exact match.filter concept i used above not filtering based on exact match.

4
  • 1
    This page documents how to create your own angular filter scotch.io/tutorials/building-custom-angularjs-filters If its a filter for checking h_id of an object you could create a h_id filter and pass it your Array of objects as the first param and the ID as the second param and then in your $filter use YOUR_ARRAY.filter(item => item.hi_id === ID PARAM) Commented Feb 23, 2018 at 9:16
  • I want to match exact id.@AlekseySolovey Commented Feb 23, 2018 at 9:18
  • Loop your data in controller and assign proper ID Commented Feb 23, 2018 at 9:19
  • @selvakumar your examples don't make sense Commented Feb 23, 2018 at 9:20

2 Answers 2

1

Use Array.filter() to get results matching certain condition.

var data = [{
  "h_id": "31",
  "city": "hill",
}, {
  "h_id": "13",
  "city": "Bevery Hills",
}, {
  "h_id": "5",
  "city": "New York",
}, {
  "h_id": "31",
  "city": "New York",
}, {
  "h_id": "5",
  "city": "New York",
}];

var newData = data.filter(el => el.h_id==="31");
console.log(newData);

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

Comments

1

Use the third parameter to strict comparison:

$scope.data1 = $filter('filter')($scope.data , {h_id:31}, true )

Here you have the documentation

Comments

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.