Reputation: 3579
I am having a json object which is by default has 12 objects user can add value to the object.If the user adds value only for 2 objects and when i tried to use every() it returns false even though the condition which i have set as true because of the empty values in the object.
I would like to know how to exclude an object which has empty value.
vm.schemeApply.documents.filter(function(doc) {
$log.log("base64", base64MimeType(doc.url));
if (doc.url && doc.url.length > 0) {
return (
base64MimeType(doc.url) === ".png" ||
base64MimeType(doc.url) === ".pdf" ||
base64MimeType(doc.url) === ".jpeg"
);
}
});
Array Example:
[
{name: "Test1", url: "AADDfdfdfdfdfdfdfdfdf"},
{name: "Test2", url: "AADDfdfdfdfdfdfdfdfdf"},
{name: "Test3", url: ""},
{name: "Test4", url: ""},
{name: "Test5", url: ""},
{name: "Test6", url: ""},
{name: "Test7", url: ""},
{name: "Test8", url: ""},
{name: "Test9", url: ""},
{name: "Test10", url: ""}
]
Upvotes: 0
Views: 817
Reputation: 1403
Looks like you're returning in the wrong spot. Your if statement receives a return, but you're never returning anything to the filter method. A slightly more terse example:
const docs = [
{name: "Test1", url: "123"},
{name: "Test2", url: "124"},
{name: "Test3", url: ""},
{name: "Test4", url: ""},
{name: "Test5", url: ""},
{name: "Test6", url: ""},
{name: "Test7", url: ""},
{name: "Test8", url: ""},
{name: "Test9", url: ""},
{name: "Test10", url: ""}
]
const result = docs.filter(function(doc) {
return !!doc.url && (
doc.url === '123' ||
doc.url === '124'
)
});
console.log(result)
Or, if you prefer using the if statement, you could also write it as:
const result = docs.filter(function(doc) {
const hasDoc = !!doc.url
let result
if (hasDoc) { result =
doc.url === '123' ||
doc.url === '124'
}
return result
})
Upvotes: 0
Reputation: 5250
You could use filter() casting the value of url
to true or false using !!
like newArr = arr.filter(o=>(!!o.url)):
, or just:
const arr = [
{name: "Test1", url: "AADDfdfdfdfdfdfdfdfdf"},
{name: "Test2", url: "AADDfdfdfdfdfdfdfdfdf"},
{name: "Test3", url: ""},
{name: "Test4", url: ""},
{name: "Test5", url: ""},
{name: "Test6", url: ""},
{name: "Test7", url: ""},
{name: "Test8", url: ""},
{name: "Test9", url: ""},
{name: "Test10", url: ""}
]
newArr = arr.filter(o=>(o.url))
console.log(newArr)
every() is not useful for you in this situation I think, because returns true (or false) whether:
all elements in the array pass the test implemented by the provided function.
Upvotes: 3
Reputation: 1357
this may be not the best solution but :
var returnValue = false;
vm.schemeApply.documents.filter(function(doc) {
$log.log("base64", base64MimeType(doc.url));
if (doc.url && doc.url.length > 0) {
let condition = ((base64MimeType(doc.url) === ".png") || (base64MimeType(doc.url) === ".pdf") || (base64MimeType(doc.url) === ".jpeg"));
if(condition) {
returnValue = true;
}
return condition;
}
return returnValue;
});
what i've done here is if only one object exist then the next object with empty url will return true
Upvotes: 0