1

I want to filter the let data array. I created map function with some condition for filter the let data array value to get the result ["true","false","true"] but I also want to display the let data array value when the result is true.

Here is my code:

 let data = 
{
    "days": {
        "Monday": {
            "checkin": "23:00",
            "checkout": "10:00"
        },
        "Tuesday": {
            "checkin": "07:00",
            "checkout": "14:00"
        },
        "Wednesdy": {
            "checkin": "07:00",
            "checkout": "04:00"
        },
    }
}
let day = ['Monday','Tuesday','Wednesdy'];
const ds = Object.keys(data.days);

const getin = (day) =day.map (d=> ds.indexOf(d)>=0 && data.days[d].checkin >= data.days[d].checkout); 


console.log("===Results===",getin);

JSFiddle Link

4 Answers 4

1

If I'm not wrong, you want to filter then return all items matching condition.

You can use Object.entries along with .reduce to achieve it.

let data = 
{
    "days": {
        "Monday": {
            "checkin": "23:00",
            "checkout": "10:00"
        },
        "Tuesday": {
            "checkin": "07:00",
            "checkout": "14:00"
        },
        "Wednesdy": {
            "checkin": "07:00",
            "checkout": "04:00"
        },
    }
}
let day = ['Monday','Tuesday','Wednesdy'];
var result = Object.entries(data.days).reduce((acc, [key, value]) => {
  if(day.indexOf(key) >= 0 && value.checkin >= value.checkout)
    acc.push({[key]: value});
    
  return acc;
}, []); 
console.log(result);

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

Comments

1

I'm not sure why you're stuck at this stage since it is quite trivial.

In this line:

const getin = (day) =day.map (d=> ds.indexOf(d)>=0 && data.days[d].checkin >= data.days[d].checkout);

you are essentially allotting the result of ds.indexOf(d)>=0 && data.days[d].checkin >= data.days[d].checkout to getin and day.

ds.indexOf(d)>=0 && data.days[d].checkin >= data.days[d].checkout is a boolean since it is the output of two checks.

You could use something like this to use the check instead of returning the check itself: ds.indexOf(d)>=0 && data.days[d].checkin >= data.days[d].checkout ? d : false. ? is a condition operator which helps reduce typing if else statements.

The modified code:

 let data = 
{
    "days": {
        "Monday": {
            "checkin": "23:00",
            "checkout": "10:00"
        },
        "Tuesday": {
            "checkin": "07:00",
            "checkout": "14:00"
        },
        "Wednesdy": {
            "checkin": "07:00",
            "checkout": "04:00"
        },
    }
}
let day = ['Monday','Tuesday','Wednesdy'];
const ds = Object.keys(data.days);

const getin = (day) =day.map (d=> ds.indexOf(d)>=0 && data.days[d].checkin >= data.days[d].checkout ? d : false); 


console.log("===Results===",getin);

Comments

0

You can do the following by pushing the object in a new array when the filter condition is true,

 let data = 
{
    "days": {
        "Monday": {
            "checkin": "23:00",
            "checkout": "10:00"
        },
        "Tuesday": {
            "checkin": "07:00",
            "checkout": "14:00"
        },
        "Wednesdy": {
            "checkin": "07:00",
            "checkout": "04:00"
        },
    }
}
let day = ['Monday','Tuesday','Wednesdy'];
const ds = Object.keys(data.days);
const daysRes = [];
const getin = (day) =day.map (d=> {
  if(ds.indexOf(d)>=0 && data.days[d].checkin >= data.days[d].checkout) {
    daysRes.push(data.days[d]);
    return true;
  }
  return false;
}); 


console.log("===Results===",getin);
console.log("===ResultsDays===",daysRes);

Comments

0

Use array.filter instead of array.map.

let data = {
  days: {
    Monday: {
      checkin: "23:00",
      checkout: "10:00"
    },
    Tuesday: {
      checkin: "07:00",
      checkout: "14:00"
    },
    Wednesdy: {
      checkin: "07:00",
      checkout: "04:00"
    }
  }
};
let day = ["Monday", "Tuesday", "Wednesdy"];
const ds = Object.keys(data.days);

const getin = Object.values(data.days).filter(d => (d.checkin >= d.checkout));

console.log("===Results===", getin);

Why your code returns ["true","false","true"]?

You are making use of array.map you are returning the result of the operation ds.indexOf(d)>=0 && data.days[d].checkin >= data.days[d].checkout This response is always boolean, either true or false.

array.map always returns a value corresponding to each node in the array. In your case it returned a boolean value after the comparison.

Go for array.filter, this will create a new array from an existing one by returning the entire node of the array dependig on a condition, which is your actual requirement.

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.