0

I have this array of objects and I'm trying to retrieve the student that match a criteria where active is true and id or token not equal to null. The result I get has token == null. Not sure what i'm doing wrongly please.

books = [{
    student: {
      studentInformation: [{
        active: true,
        id: "92839"
      }]
    }
  },
  {
    student: {
      studentInformation: [{
        active: true,
        token: null
      }]
    }
  },
  {
    student: {
      studentInformation: [{
        active: false,
        token: "eytqwedgd"
      }]
    }
  },
  {
    student: {
      studentInformation: [{
        active: true,
        id: null
      }]
    }
  }
]


let students = books.filter(stu =>
  stu.student.studentInformation.every(y => y.active ===
    true && (y.id !== null || y.token !== null)))

console.log(students);
Barmar
  • 741,623
  • 53
  • 500
  • 612
Hopez
  • 141
  • 1
  • 9
  • `||` should be `&&`. But you don't really need to use both `!==` and `!=`, just use `!=`. – Barmar Sep 06 '18 at 17:21
  • @Barmar: "and id OR token not equal to null." is a little hard to interpret, but it's reasonable to guess that this meant something like `('id' in info && info.id !== null) || ('token' in info && token.info !== null)` In that case, this is clearly not a duplicate of that question. – Scott Sauyet Sep 06 '18 at 17:50
  • Oops, for some reason I thought he was comparing the same variable, once with `!=` and then with `!==`. Not sure how I misread that. – Barmar Sep 06 '18 at 18:45

2 Answers2

0

Just use && which return true when token and id not equal to null.

var books = [{
        student: {
            studentInformation: [{
                active: true,
                id: "92839"
            }]
        }
    },
    {
        student: {
            studentInformation: [{
                active: true,
                token: null
            }]
        }
    },
    {
        student: {
            studentInformation: [{
                active: false,
                token: "eytqwedgd"
            }]
        }
    },
    {
        student: {
            studentInformation: [{
                active: true,
                id: null
            }]
        }
    }
]


let students = books.filter(stu =>
    (stu.student.studentInformation.every(y => (y.active = true && (y.id !== null && y.token !== null)))));
    
console.log(students);
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29
0

You may like to use reduce & forEach. Inside reduce callback iterate over studentInformation and check the condition before pushing it to the accumulator array

let books = [
  {
    student: {
      studentInformation: [
        {
          active: true,
          id: "92839"
        }
      ]
    }
  },
  {
    student: {
      studentInformation: [
        {
          active: true,
          token: null
        }
      ]
    }
  },
  {
    student: {
      studentInformation: [
        {
          active: false,
          token: "eytqwedgd"
        }
      ]
    }
  },
  {
    student: {
      studentInformation: [
        {
          active: true,
          id: null
        }
      ]
    }
  }
]
console.log(books)

let students = books.reduce(function(acc,curr){
   curr.student.studentInformation.forEach(function(item){
     if(item.active && item.token !==null && item.id !==null){
       acc.push(item)
     }
   }) 
return acc;

},[])
console.log(students)
brk
  • 48,835
  • 10
  • 56
  • 78