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
javascript angularjs
add a comment |
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
javascript angularjs
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
add a comment |
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
javascript angularjs
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
javascript angularjs
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
add a comment |
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
add a comment |
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)
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
|
show 2 more comments
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
add a comment |
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)
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
|
show 2 more comments
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)
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
|
show 2 more comments
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)
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)
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
|
show 2 more comments
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
|
show 2 more comments
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
add a comment |
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
add a comment |
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
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
answered Nov 22 at 14:18
BartoszTermena
48629
48629
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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