1

I got an array of objects Course.

export interface Course {
    id: string;
    student: User[];
    color: string;
}
export interface User {
    id: string;
    name: string;
    //...
}

I need to delete all element of the Course array, if the the student array inside him contain specific id

I was trying to do something like that but I don't understand what I'm suppose to do inside the filter part.

const deleteCourseWhereImIn = (courses: Course[]) => {
    const myid = "1111"
    return courses.filter((item):Course => 
        //[...]
    )
};

example:

the input:

const courses = [
    {
        id : "JSLKLDK",
        student: [
            {
                id: "1111",
                name: "John"
            },
            {
                id: "2222",
                name: "Ronald"
            },
            {
                id: "3333",
                name: "Tim"
            },
       ],
       color: "#f01000",
    },
    {
        id : "5JLDKLD",
        student: [
            {
                id: "2222",
                name: "Ronald"
            },
            {
                id: "1111",
                name: "John"
            },
       ],
       color: "#511131",
    },
    {
        id : "PDLFMLZ",
        student: [
            {
                id: "2222",
                name: "Ronald"
            },
       ],
       color: "#ffffff",
    },
]

my ID is 1111

the output should be:

const courses = [
    {
        id : "PDLFMLZ",
        student: [
            {
                id: "2222",
                name: "Ronald"
            },
       ],
       color: "#ffffff",
    },
]

because I'm john and I'm not in this course.

6 Answers 6

2

Using filter and every:

const myID = "1111";

const courses = [
    {
        id : "JSLKLDK",
        student: [
            {
                id: "1111",
                name: "John"
            },
            {
                id: "2222",
                name: "Ronald"
            },
            {
                id: "3333",
                name: "Tim"
            },
       ],
       color: "#f01000",
    },
    {
        id : "5JLDKLD",
        student: [
            {
                id: "2222",
                name: "Ronald"
            },
            {
                id: "1111",
                name: "John"
            },
       ],
       color: "#511131",
    },
    {
        id : "PDLFMLZ",
        student: [
            {
                id: "2222",
                name: "Ronald"
            },
       ],
       color: "#ffffff",
    },
]

const filtered = courses.filter(c => c.student.every(s => s.id !== myID))

console.log(filtered);

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

Comments

1

Just use functional features of javascript.

const courses = [... your courses ...]
const idToCheck = "1111"
let newCourses = courses.filter(x => 
    x.student.findIndex(y => y.id == idToCheck) === -1)

Comments

1

Better use reduce to generate a new array and assign it to Course

let courses = [
    {
        id : "JSLKLDK",
        student: [
            {
                id: "1111",
                name: "John"
            },
            {
                id: "2222",
                name: "Ronald"
            },
            {
                id: "3333",
                name: "Tim"
            },
       ],
       color: "#f01000",
    },
    {
        id : "5JLDKLD",
        student: [
            {
                id: "2222",
                name: "Ronald"
            },
            {
                id: "1111",
                name: "John"
            },
       ],
       color: "#511131",
    },
    {
        id : "PDLFMLZ",
        student: [
            {
                id: "2222",
                name: "Ronald"
            },
       ],
       color: "#ffffff",
    },
]
courses = courses.reduce((arr, c)=>{
  if(!c.student.some((s)=>(s.id==='1111'))){
    arr.push(c);
  }
  return arr
},[])
console.log(courses);

5 Comments

Please don't post just code in answers; it's far better to explain them, so the OP can learn something.
I will update the answer, if you like you can suggest edit
@Mitya thankx for the down vote I think I will stop answer questions all together in the future
Don't take it so personally. Your answer had no explanation, and so was perfectly worthy of a downvote. You have now added some (admittedly minimal) explanation so I have retracted the downvote.
Why are you asking me to edit? It's not for me to explain your answer for you, or for anyone else. You've done that now, the downvote is retracted, let's move on.
1

Using filter and findIndex:

const deleteCourseWhereImIn = (courses: Course[]) => {
    const myid = "1111";
    return courses.filter(item =>
        return item.student.findIndex(user => user.id == myid) === -1
    );
};

And here is a detailed version to better illustrate what is happening:

const deleteCourseWhereImIn = (courses: Course[]) => {
    const myid = "1111";

    const filteredCourses = courses.filter(item: Course => {
        const index = item.student.findIndex(user: User => {
             return user.id == myid
        });

        // The method findIndex() will return -1 if the value you search for does not appear in the array.
        return index === -1;
    });

    return filteredCourses;
};

Comments

0

You can filter the courses with the condition id: "1111"by using filter and some. In your description you mention that you want to delete the courses that your are in.

For doing that, you can use this filter in your function deleteCourseWhereImIn and assign what it returns to your Course[].

let courses = [
    {
        id : "JSLKLDK",
        student: [
            {
                id: "1111",
                name: "John"
            },
            {
                id: "2222",
                name: "Ronald"
            },
            {
                id: "3333",
                name: "Tim"
            },
       ],
       color: "#f01000",
    },
    {
        id : "5JLDKLD",
        student: [
            {
                id: "2222",
                name: "Ronald"
            },
            {
                id: "1111",
                name: "John"
            },
       ],
       color: "#511131",
    },
    {
        id : "PDLFMLZ",
        student: [
            {
                id: "2222",
                name: "Ronald"
            },
       ],
       color: "#ffffff",
    },
]

courses = courses.filter(course => {
    return !course.student.some(student => student.id === "1111");
});


console.log(courses);

Comments

0

Please try this.
I am passing the array of courses in the function deleteCourseWhereImIn() and then say myId is "1111". I am checking if any student in the student array has this id, and thus returning boolean value by using some() method. Then filter that particular course based on true or false.

    const deleteCourseWhereImIn = (courses: Course[]) => {
      const myId = "1111";
      let filteredCourses = courses.filter(course => {
        let studentArr = course.student;
        let filterdStudent = studentArr.some(student => student.id === myId);
        return !filterdStudent;
        
      });
    
      console.log('Filtered Course', filteredCourses);
    }
    
    deleteCourseWhereImIn(courses);  // calling function 

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.