0

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: ""} 
]

3 Answers 3

3

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.

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

2 Comments

Isn't OP's function already doing this? He has additional mime-type checks inside the "url exists" check. His error is that his filter callback is not returning anything.
@FranCarstens yes, but OP adds doc.url.length > 0 that I think is useless, and asks about every()
0

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

Comments

0

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
})

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.