1

How to filter index value in foreach Javascript with condition if

last_login = null

then store the filtered values to

this.searchLogin

Image below is the ouput of: console.log(obj);

enter image description here

Here' what I've done so far:

try {
   this.searchTrigger = true
   var obj = this.list;

   this.searchLogin = [];

   for (let i=0; i < obj.length; ++i){
       if(obj.filter( x => obj[i].profile.last_login === null)){
          this.searchLogin.push(obj[i]);
       }
   }

console.log('This are obj data passed to search login',this.searchLogin);
2
  • Do you want to searchLogin to contain the object or obj[i].profile.last_login? Commented Jun 19, 2019 at 6:14
  • @NikhilAggarwal Yes I want to push new filtered array values to searchLogin, with the condition of if profile.last_login = null Commented Jun 19, 2019 at 6:18

3 Answers 3

1

var obj = [
{ profile: { last_login: null } },
{ profile: { last_login: true } },
{ profile: { last_login: null } },
{ profile: { last_login: true } },
{ profile: { last_login: null } }]



//With filter and map
console.log("With filter and map: ", 
        obj.map((x, i) => ({ ...x.profile, i }))
            .filter(x => !x.last_login)
            .map(x => x.i));



//With reduce
console.log("With reduce: ",
        obj.reduce((p, c, i) => (!c.profile.last_login && p.push(i), p), [])
        )

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

Comments

0

I think no need to save the results explicitly to an array again, the filter on array by itself will create an results array based on the filter condition.

For more on filter,

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

In your case,

 this.searchLogin = obj.filter( x => x.profile.last_login === null);

would do the trick.

4 Comments

Does this check all the index array values here in this.list? Thanks :) Will try this out.
filter checks all the values in the array. please try out let know.
try { this.searchTrigger = true var obj = this.list; this.searchLogin = obj.filter( x => x.profile.last_login === null); console.log('This are obj data passed to search login',this.searchLogin); }
stackblitz.com/edit/js-heigj7 created a stackblitz for you with dummy values to replicate your object list and the expected results. Please free to edit the same and share with your list values if its still not working
0

There are some points that need to be noted

  • filter function will return an array and even if it empty (no matched results), it will still be evaluated as true i.e. your if condition will always be executed
  • The if condition has a filter function inside a for loop which means that you are unnecessarily checking condition for same object for all the objects in array i.e. for each object the filter function will either return empty array or array of all the objects.

You can use Array.reduce and store the last_login information in the resultant array

this.searchLogin = this.list.reduce((a,c) => c.profile.last_login === null ? a.concat(c.profile.last_login) : a, []);

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.