2

I have an array, say

[
    {
        lesson: 1,
        title: "Welcome",
        slug: "welcome",
        active: active,
        breakDay: false,
        started_time: new Date(),
        finished_time: null,
        completed: true,
        sublesson: [
            {
                lesson: 1.1,
                title: "Evaluation",
                slug: 'evaluation',
                completed: false,
                answers: []
            }
        ],
        answers: []
    },
    {
        lesson: 2,
        title: "Example",
        slug: "example",
        active: active,
        breakDay: false,
        started_time: null,
        finished_time: null,
        completed: false,
        sublesson: [
            {
                lesson: 2.1,
                title: "example2,
                slug: 'example2,
                answers: []
            }
        ],
        answers: []
    }
]

I need to find a way to map out the first item to reach specific conditions.

I tried inside my React App, but quickly realised it's actually checking if those conditions are true on the first item and not the whole array.

    {
        lessons !== null ?

            lessons.map((item, index, arr) => (
                index === 0 && item.active === true ?
                <>
                <p>{item.title}</p>
                </>
                :
                null
            ))
            :
            null

    }

So essentially, how can I filter the first item, to meet specific conditions eg item.active === true && item.completed === false ?

1

2 Answers 2

7

You may need filter here:

var data=[ { lesson: 1, title: "Welcome", slug: "welcome", active: 'active', breakDay: false, started_time: new Date(), finished_time: null, completed: true, sublesson: [ { lesson: 1.1, title: "Evaluation", slug: 'evaluation', completed: false, answers: [] } ], answers: [] }, { lesson: 2, title: "Example", slug: "example", active: 'active', breakDay: false, started_time: null, finished_time: null, completed: false, sublesson: [ { lesson: 2.1, title: "example2", slug: 'example2', answers: [] } ], answers: [] }];

var result = data.filter(({active, completed})=> active && !completed);

console.log(result);

Or find to get first matching object:

var data=[ { lesson: 1, title: "Welcome", slug: "welcome", active: 'active', breakDay: false, started_time: new Date(), finished_time: null, completed: true, sublesson: [ { lesson: 1.1, title: "Evaluation", slug: 'evaluation', completed: false, answers: [] } ], answers: [] }, { lesson: 2, title: "Example", slug: "example", active: 'active', breakDay: false, started_time: null, finished_time: null, completed: false, sublesson: [ { lesson: 2.1, title: "example2", slug: 'example2', answers: [] } ], answers: [] }];

var result = data.find(({active, completed})=> active && !completed);

console.log(result);

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

Comments

2

const arr = [
    {
        lesson: 1,
        title: "Welcome",
        slug: "welcome",
        active: false,
    },
    {
        lesson: 2,
        title: "Welcome 2",
        slug: "welcome",
        active: true,
    },
    {
        lesson: 3,
        title: "Example 3",
        slug: "example",
        active: true,
    }
]

console.log(arr.find(item => item.active))

I think you can use the Array.prototype.find function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

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.