How do i filter an array inside of a array of objects using filter?











up vote
2
down vote

favorite












I have an array of products and models that I'm using 'filter'






var app = angular.module("myApp", );
app.controller("myCtrl", function($scope) {

$scope.products = [{
'id': 1,
'category': 'Tv',
'models': [{
'modelno': 12,
'modelname': 'ASF456'

},
{
'modelno': 13,
'modelname': 'Aip456'

}
]
},

{
'id': 2,
'category': 'Mobile',
'models': [{
'modelno': 21,
'modelname': 'FGH74'

},
{
'modelno': 22,
'modelname': 'UIO06'

}
]

}

];

$scope.search = '';
$scope.filterData = function() {
return $scope.products.filter(function(item) {

return (item.id.toString().indexOf($scope.search) > -1

||
(item.category.toLowerCase().indexOf($scope.search)) > -1)



});

}
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="search">
<h1 ng-repeat="x in filterData() | filter :search">{{x.id}} {{x.category}}</h1>
</body>





to filter these products by id and category.
The filter is working but i want to add one more field inside filter modelname .



How do I set the filter to only apply to the id,category and modelname field of my array rather than every field?



How do I set the filter to only apply to the id,category and modelname field of my array rather than every field?



i want to filter by id category ,modelname



now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem










share|improve this question
























  • What's wrong with extending your current approach?
    – charlietfl
    Nov 22 at 14:12












  • nothing wrong i want to filter one more field 'modelname' i dont know how to add this @charlietfl
    – komal
    Nov 22 at 14:18










  • Did you try just adding another ||?
    – charlietfl
    Nov 22 at 14:21










  • we can't do like that bcoz again models is array @charlietfl
    – komal
    Nov 22 at 14:30















up vote
2
down vote

favorite












I have an array of products and models that I'm using 'filter'






var app = angular.module("myApp", );
app.controller("myCtrl", function($scope) {

$scope.products = [{
'id': 1,
'category': 'Tv',
'models': [{
'modelno': 12,
'modelname': 'ASF456'

},
{
'modelno': 13,
'modelname': 'Aip456'

}
]
},

{
'id': 2,
'category': 'Mobile',
'models': [{
'modelno': 21,
'modelname': 'FGH74'

},
{
'modelno': 22,
'modelname': 'UIO06'

}
]

}

];

$scope.search = '';
$scope.filterData = function() {
return $scope.products.filter(function(item) {

return (item.id.toString().indexOf($scope.search) > -1

||
(item.category.toLowerCase().indexOf($scope.search)) > -1)



});

}
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="search">
<h1 ng-repeat="x in filterData() | filter :search">{{x.id}} {{x.category}}</h1>
</body>





to filter these products by id and category.
The filter is working but i want to add one more field inside filter modelname .



How do I set the filter to only apply to the id,category and modelname field of my array rather than every field?



How do I set the filter to only apply to the id,category and modelname field of my array rather than every field?



i want to filter by id category ,modelname



now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem










share|improve this question
























  • What's wrong with extending your current approach?
    – charlietfl
    Nov 22 at 14:12












  • nothing wrong i want to filter one more field 'modelname' i dont know how to add this @charlietfl
    – komal
    Nov 22 at 14:18










  • Did you try just adding another ||?
    – charlietfl
    Nov 22 at 14:21










  • we can't do like that bcoz again models is array @charlietfl
    – komal
    Nov 22 at 14:30













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have an array of products and models that I'm using 'filter'






var app = angular.module("myApp", );
app.controller("myCtrl", function($scope) {

$scope.products = [{
'id': 1,
'category': 'Tv',
'models': [{
'modelno': 12,
'modelname': 'ASF456'

},
{
'modelno': 13,
'modelname': 'Aip456'

}
]
},

{
'id': 2,
'category': 'Mobile',
'models': [{
'modelno': 21,
'modelname': 'FGH74'

},
{
'modelno': 22,
'modelname': 'UIO06'

}
]

}

];

$scope.search = '';
$scope.filterData = function() {
return $scope.products.filter(function(item) {

return (item.id.toString().indexOf($scope.search) > -1

||
(item.category.toLowerCase().indexOf($scope.search)) > -1)



});

}
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="search">
<h1 ng-repeat="x in filterData() | filter :search">{{x.id}} {{x.category}}</h1>
</body>





to filter these products by id and category.
The filter is working but i want to add one more field inside filter modelname .



How do I set the filter to only apply to the id,category and modelname field of my array rather than every field?



How do I set the filter to only apply to the id,category and modelname field of my array rather than every field?



i want to filter by id category ,modelname



now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem










share|improve this question















I have an array of products and models that I'm using 'filter'






var app = angular.module("myApp", );
app.controller("myCtrl", function($scope) {

$scope.products = [{
'id': 1,
'category': 'Tv',
'models': [{
'modelno': 12,
'modelname': 'ASF456'

},
{
'modelno': 13,
'modelname': 'Aip456'

}
]
},

{
'id': 2,
'category': 'Mobile',
'models': [{
'modelno': 21,
'modelname': 'FGH74'

},
{
'modelno': 22,
'modelname': 'UIO06'

}
]

}

];

$scope.search = '';
$scope.filterData = function() {
return $scope.products.filter(function(item) {

return (item.id.toString().indexOf($scope.search) > -1

||
(item.category.toLowerCase().indexOf($scope.search)) > -1)



});

}
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="search">
<h1 ng-repeat="x in filterData() | filter :search">{{x.id}} {{x.category}}</h1>
</body>





to filter these products by id and category.
The filter is working but i want to add one more field inside filter modelname .



How do I set the filter to only apply to the id,category and modelname field of my array rather than every field?



How do I set the filter to only apply to the id,category and modelname field of my array rather than every field?



i want to filter by id category ,modelname



now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem






var app = angular.module("myApp", );
app.controller("myCtrl", function($scope) {

$scope.products = [{
'id': 1,
'category': 'Tv',
'models': [{
'modelno': 12,
'modelname': 'ASF456'

},
{
'modelno': 13,
'modelname': 'Aip456'

}
]
},

{
'id': 2,
'category': 'Mobile',
'models': [{
'modelno': 21,
'modelname': 'FGH74'

},
{
'modelno': 22,
'modelname': 'UIO06'

}
]

}

];

$scope.search = '';
$scope.filterData = function() {
return $scope.products.filter(function(item) {

return (item.id.toString().indexOf($scope.search) > -1

||
(item.category.toLowerCase().indexOf($scope.search)) > -1)



});

}
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="search">
<h1 ng-repeat="x in filterData() | filter :search">{{x.id}} {{x.category}}</h1>
</body>





var app = angular.module("myApp", );
app.controller("myCtrl", function($scope) {

$scope.products = [{
'id': 1,
'category': 'Tv',
'models': [{
'modelno': 12,
'modelname': 'ASF456'

},
{
'modelno': 13,
'modelname': 'Aip456'

}
]
},

{
'id': 2,
'category': 'Mobile',
'models': [{
'modelno': 21,
'modelname': 'FGH74'

},
{
'modelno': 22,
'modelname': 'UIO06'

}
]

}

];

$scope.search = '';
$scope.filterData = function() {
return $scope.products.filter(function(item) {

return (item.id.toString().indexOf($scope.search) > -1

||
(item.category.toLowerCase().indexOf($scope.search)) > -1)



});

}
});

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

<body ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="search">
<h1 ng-repeat="x in filterData() | filter :search">{{x.id}} {{x.category}}</h1>
</body>






javascript angularjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 14:23

























asked Nov 22 at 13:56









komal

133




133












  • What's wrong with extending your current approach?
    – charlietfl
    Nov 22 at 14:12












  • nothing wrong i want to filter one more field 'modelname' i dont know how to add this @charlietfl
    – komal
    Nov 22 at 14:18










  • Did you try just adding another ||?
    – charlietfl
    Nov 22 at 14:21










  • we can't do like that bcoz again models is array @charlietfl
    – komal
    Nov 22 at 14:30


















  • What's wrong with extending your current approach?
    – charlietfl
    Nov 22 at 14:12












  • nothing wrong i want to filter one more field 'modelname' i dont know how to add this @charlietfl
    – komal
    Nov 22 at 14:18










  • Did you try just adding another ||?
    – charlietfl
    Nov 22 at 14:21










  • we can't do like that bcoz again models is array @charlietfl
    – komal
    Nov 22 at 14:30
















What's wrong with extending your current approach?
– charlietfl
Nov 22 at 14:12






What's wrong with extending your current approach?
– charlietfl
Nov 22 at 14:12














nothing wrong i want to filter one more field 'modelname' i dont know how to add this @charlietfl
– komal
Nov 22 at 14:18




nothing wrong i want to filter one more field 'modelname' i dont know how to add this @charlietfl
– komal
Nov 22 at 14:18












Did you try just adding another ||?
– charlietfl
Nov 22 at 14:21




Did you try just adding another ||?
– charlietfl
Nov 22 at 14:21












we can't do like that bcoz again models is array @charlietfl
– komal
Nov 22 at 14:30




we can't do like that bcoz again models is array @charlietfl
– komal
Nov 22 at 14:30












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You can add another or condition like this



 || (item.models !== null && item.models.length > 0 && item.models.filter(e => {return e.modelname.search($scope.search) > 0 }).length > -1)





share|improve this answer























  • can you run my code based on user enter value we need to filter
    – komal
    Nov 22 at 14:19










  • @komal I don't understand what do you want
    – FarukT
    Nov 22 at 14:23










  • based on user enter value want to filter by id, category ,modelname now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem
    – komal
    Nov 22 at 14:25










  • @komal can you test now?
    – FarukT
    Nov 22 at 14:32










  • no its not working
    – komal
    Nov 22 at 14:39


















up vote
0
down vote













Do i understood correctly - you want to filter your $scope.products by means of id ? if so try like this:



<input ng-model="id " type="number" min="0"/>
<br />
<div ng-repeat="x in products | IDFiletr:id">
{{x.category}}
<br />
</div>


JS:



app.filter('IDFiletr',function(){
return function(data, id) {
if (!id) return data;
var ids = ;
angular.forEach(data, function(item){
if(item.id === id) {
ids.push(item);
}
});
return ids;
};
});


plunker: http://plnkr.co/edit/q9DsvZP4scA1oKsJj3Vu?p=preview






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432568%2fhow-do-i-filter-an-array-inside-of-a-array-of-objects-using-filter%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You can add another or condition like this



     || (item.models !== null && item.models.length > 0 && item.models.filter(e => {return e.modelname.search($scope.search) > 0 }).length > -1)





    share|improve this answer























    • can you run my code based on user enter value we need to filter
      – komal
      Nov 22 at 14:19










    • @komal I don't understand what do you want
      – FarukT
      Nov 22 at 14:23










    • based on user enter value want to filter by id, category ,modelname now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem
      – komal
      Nov 22 at 14:25










    • @komal can you test now?
      – FarukT
      Nov 22 at 14:32










    • no its not working
      – komal
      Nov 22 at 14:39















    up vote
    1
    down vote



    accepted










    You can add another or condition like this



     || (item.models !== null && item.models.length > 0 && item.models.filter(e => {return e.modelname.search($scope.search) > 0 }).length > -1)





    share|improve this answer























    • can you run my code based on user enter value we need to filter
      – komal
      Nov 22 at 14:19










    • @komal I don't understand what do you want
      – FarukT
      Nov 22 at 14:23










    • based on user enter value want to filter by id, category ,modelname now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem
      – komal
      Nov 22 at 14:25










    • @komal can you test now?
      – FarukT
      Nov 22 at 14:32










    • no its not working
      – komal
      Nov 22 at 14:39













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    You can add another or condition like this



     || (item.models !== null && item.models.length > 0 && item.models.filter(e => {return e.modelname.search($scope.search) > 0 }).length > -1)





    share|improve this answer














    You can add another or condition like this



     || (item.models !== null && item.models.length > 0 && item.models.filter(e => {return e.modelname.search($scope.search) > 0 }).length > -1)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 22 at 14:42

























    answered Nov 22 at 14:14









    FarukT

    40719




    40719












    • can you run my code based on user enter value we need to filter
      – komal
      Nov 22 at 14:19










    • @komal I don't understand what do you want
      – FarukT
      Nov 22 at 14:23










    • based on user enter value want to filter by id, category ,modelname now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem
      – komal
      Nov 22 at 14:25










    • @komal can you test now?
      – FarukT
      Nov 22 at 14:32










    • no its not working
      – komal
      Nov 22 at 14:39


















    • can you run my code based on user enter value we need to filter
      – komal
      Nov 22 at 14:19










    • @komal I don't understand what do you want
      – FarukT
      Nov 22 at 14:23










    • based on user enter value want to filter by id, category ,modelname now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem
      – komal
      Nov 22 at 14:25










    • @komal can you test now?
      – FarukT
      Nov 22 at 14:32










    • no its not working
      – komal
      Nov 22 at 14:39
















    can you run my code based on user enter value we need to filter
    – komal
    Nov 22 at 14:19




    can you run my code based on user enter value we need to filter
    – komal
    Nov 22 at 14:19












    @komal I don't understand what do you want
    – FarukT
    Nov 22 at 14:23




    @komal I don't understand what do you want
    – FarukT
    Nov 22 at 14:23












    based on user enter value want to filter by id, category ,modelname now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem
    – komal
    Nov 22 at 14:25




    based on user enter value want to filter by id, category ,modelname now these two fields id ,category filter is done but i want to add modelname also but modelname is inside models array here i am facing problem
    – komal
    Nov 22 at 14:25












    @komal can you test now?
    – FarukT
    Nov 22 at 14:32




    @komal can you test now?
    – FarukT
    Nov 22 at 14:32












    no its not working
    – komal
    Nov 22 at 14:39




    no its not working
    – komal
    Nov 22 at 14:39












    up vote
    0
    down vote













    Do i understood correctly - you want to filter your $scope.products by means of id ? if so try like this:



    <input ng-model="id " type="number" min="0"/>
    <br />
    <div ng-repeat="x in products | IDFiletr:id">
    {{x.category}}
    <br />
    </div>


    JS:



    app.filter('IDFiletr',function(){
    return function(data, id) {
    if (!id) return data;
    var ids = ;
    angular.forEach(data, function(item){
    if(item.id === id) {
    ids.push(item);
    }
    });
    return ids;
    };
    });


    plunker: http://plnkr.co/edit/q9DsvZP4scA1oKsJj3Vu?p=preview






    share|improve this answer

























      up vote
      0
      down vote













      Do i understood correctly - you want to filter your $scope.products by means of id ? if so try like this:



      <input ng-model="id " type="number" min="0"/>
      <br />
      <div ng-repeat="x in products | IDFiletr:id">
      {{x.category}}
      <br />
      </div>


      JS:



      app.filter('IDFiletr',function(){
      return function(data, id) {
      if (!id) return data;
      var ids = ;
      angular.forEach(data, function(item){
      if(item.id === id) {
      ids.push(item);
      }
      });
      return ids;
      };
      });


      plunker: http://plnkr.co/edit/q9DsvZP4scA1oKsJj3Vu?p=preview






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Do i understood correctly - you want to filter your $scope.products by means of id ? if so try like this:



        <input ng-model="id " type="number" min="0"/>
        <br />
        <div ng-repeat="x in products | IDFiletr:id">
        {{x.category}}
        <br />
        </div>


        JS:



        app.filter('IDFiletr',function(){
        return function(data, id) {
        if (!id) return data;
        var ids = ;
        angular.forEach(data, function(item){
        if(item.id === id) {
        ids.push(item);
        }
        });
        return ids;
        };
        });


        plunker: http://plnkr.co/edit/q9DsvZP4scA1oKsJj3Vu?p=preview






        share|improve this answer












        Do i understood correctly - you want to filter your $scope.products by means of id ? if so try like this:



        <input ng-model="id " type="number" min="0"/>
        <br />
        <div ng-repeat="x in products | IDFiletr:id">
        {{x.category}}
        <br />
        </div>


        JS:



        app.filter('IDFiletr',function(){
        return function(data, id) {
        if (!id) return data;
        var ids = ;
        angular.forEach(data, function(item){
        if(item.id === id) {
        ids.push(item);
        }
        });
        return ids;
        };
        });


        plunker: http://plnkr.co/edit/q9DsvZP4scA1oKsJj3Vu?p=preview







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 14:18









        BartoszTermena

        48629




        48629






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432568%2fhow-do-i-filter-an-array-inside-of-a-array-of-objects-using-filter%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo