I'm working on an Ionic
hybrid app, but I'm quite new in Javascript
/ AngularJS
.
I have this data structure:
var objects= [
{
name: "o1",
company: {
id:2,
fields: [1,6]
}
},
{
name: "o2",
company: {
id:2,
fields: [3,4,5]
}
}
];
var selectedFields= [
{
id: 1,
name: "f1",
},
{
id: 2,
name: "f2",
}
];
I want to filter the objects array
elements which contains the ids (in the attribute company.fields
) corresponding with the elements contained in selectedFields array
.
For example, in this case, we have 2 elements, o1
and o2
. o1
contains the fields
1
and 6
, and o2
contains the fields
3
, 4
and 5
.
The selectedFields array
contains the fields
with ids
1
and 2
, so in this case the filtered object
should be the object o1
.
That's what I have tried:
var filteredObjects = filterFilter(objects, selectedFields,function(o, sf){
return angular.forEach(o.company.fields, function (field) {
return field.id == sf.is;
});
});
But it says that o is undefined
.
and:
var result = [];
angular.forEach(objects, function(o){
angular.forEach(o.company.fields, function(f){
angular.forEach(selectedFields, function(sf){
if(sf.id == f.id) {
result.push(o);
}
});
});
});
But the condition if(sf.id == f.id)
is never true
.
Any ideas? Thanks