0

Considering this array :

const body = [
  {id: 1,courses: [{title:"course1.1", results:[]}, {title:"course1.2", results:[]}]},
  {id: 2,courses: [{title:"course1.1", results:[]}, {title:"course1.2",results:[]}]},   
];

I'm trying to get the first item in body that have a course with no results.

I tried this :

body.filter(session=>session.courses.filter(course=>course.results.length<=0));

But for a reason that I don't understand it return all items.

Any helps would be really appreciated, thank you!

P.S. I already made some try with Array.some and with a simple forEach, with no luck.

Edit: I forgot to include my jsfiddle test, https://jsfiddle.net/jonathanlaf/4os0fzv1/

Edit2: I know that filter will return all results that match, and that's fine, I can just take the first index of the newly returned array.

Edit3: I actually expect the newly returned array to look like this :

const body2 = [
  {id: 2,courses: [{title:"course1.1", results:[]}, {title:"course1.2",results:[]}]},   
];
6
  • 1
    please add the wanted result as well. Commented Aug 21, 2018 at 17:30
  • @NinaScholz Just added it thank you. Commented Aug 21, 2018 at 17:33
  • try using $.grep(body,function(obj,index1){ if ($.grep(obj.courses,function(course,index2){ if ( course.results.length<=0) {return course; } }).length>0) return obj; }) Please make sure of syntax errors in my code if possible but grep can useful for you. Commented Aug 21, 2018 at 17:37
  • you can control the return result by index number. Commented Aug 21, 2018 at 17:38
  • Why would the return look like this when you have empty results in id:1? I am confused. You result only has id: 2? Commented Aug 21, 2018 at 18:53

3 Answers 3

3

This should do it:

const body = [ {id: 1,courses: [{title:"course1.1", results:[{title:"result1.1.1"}, {title:"result1.1.2"}]}, {title:"course1.2", results:[]}]}, {id: 2,courses: [{title:"course1.1", results:[]}, {title:"course1.2",results:[]}]}, {id: 3,courses: []}];

console.log(body.reduce((r,c) => (c.courses.length ? r.push({ id: c.id, courses: [...c.courses.filter(({results}) => !results.length)]}) : true) && r, []))

It uses reduce and a filter to get what you need.

Update: Added handling in the case where courses is just an empty array with no results e.g. {id: 3,courses: []}.

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

9 Comments

It's almost working, it's removing the first level of information, so I now receive just the courses with no parent id.
Oh you mean you need the id?
I added expected value in the question so you can see what I'm expecting :) Thank you for your help !
I just ran into an error, if the parent has no course with result, it still return the parent with an empty result, is there a way to get rid of it ? const body = [{id: 1, courses: [{title: "course1.1", results: []}, {title: "course1.2", results: []}]}, {id: 2, courses: [{title: "course1.1", results: []}, {title: "course1.2", results: []}]}];
Sure give me a few mins
|
1

You could map new objects without mutating the original data.

var array = [{ id: 1, courses: [{ title: "course1.1", results: [{ title: "result1.1.1" }, { title: "result1.1.2" }] }, { title: "course1.2", results: [] }] }, { id: 2, courses: [{ title: "course1.1", results: [] }, { title: "course1.2", results: [] }] }],
    result = array.map(o => 
        Object.assign(
            {},
            o,
            { courses: o.courses.filter(({ results: { length } }) => !length) }
        )
    );	
    
console.log(result);

1 Comment

Thanks for your answer @Nina :)
-1

    const body = [
    {id: 1,courses: [{title:"course1.1", results:[{title:"result1.1.1"}, {title:"result1.1.2"}]}, {title:"course1.2", results:[]}]},
    {id: 2,courses: [{title:"course1.1", results:[]}, {title:"course1.2",results:[]}]},   
    ];    
    let results = body.find(session => -1 < session.courses.findIndex(course => course.results.length <=0))
    console.log(results)

Array.find() documentation Array.findIndex()

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.