简体   繁体   中英

JavaScript -how to get array value with some condition?

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

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM