0

hi guys i have a problem here, i want to get the lowest three values from the array of javascript objects. and the array of object is like this

let array = [{student_id: 'asda123asd1', nrw: 70}, {student_id: 'asda123asd2', nrw: 80}, {student_id: 'asda123asd3', nrw: 50}, {student_id: 'asda123asd4', nrw: 50}, {student_id: 'asda123asd5', nrw: 100}]; 

the result i want is like this

[{student_id: 'asda123asd3', nrw: 50}, {student_id: 'asda123asd4', nrw: 50},{student_id: 'asda123asd1', nrw: 70}]

i have tried this but didnt solve my problem

let test = async (array, n) => {
    let empty = []
    let MAX = 100000; 
    let firstmin = MAX; 
    let secmin = MAX; 
    let thirdmin = MAX; 
        for (let i = 0; i < n;i++) 
        { 
            
            /* Check if current element is less than 
            firstmin, then update first, second and 
            third */
            if (array[i].nrw < firstmin) 
            { 
                thirdmin = secmin; 
                secmin = firstmin; 
                firstmin = array[i].nrw; 
            } 
    
            /* Check if current element is less than 
            secmin then update second and third */
            else if (array[i].nrw < secmin) 
            { 
                thirdmin = secmin; 
                secmin = array[i].nrw; 
            } 
    
            /* Check if current element is less than 
            then update third */
            else if (array[i].nrw < thirdmin) 
                thirdmin = array[i].nrw; 


        }

        if(firstmin != 100000){
            empty.push({nrw: firstmin})
        }

        if(secmin != 100000){
            empty.push({nrw: secmin})
        }

        if(thirdmin != 100000){
            empty.push({nrw: thirdmin})
        }

        console.log(empty)

}

how can you guys solve this?

0

2 Answers 2

3

I mean you can use the inbuilt sort as well with a custom comparator function and just get the first three values.

let array = [{student_id: 'asda123asd1', nrw: 70}, {student_id: 'asda123asd2', nrw: 80}, {student_id: 'asda123asd3', nrw: 50}, {student_id: 'asda123asd4', nrw: 50}, {student_id: 'asda123asd5', nrw: 100}];


array.sort((item1,item2)=>item1.nrw-item2.nrw);

console.log("First Item",array[0]);
console.log("Second Item",array[1]);
console.log("Third Item",array[2]);

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

2 Comments

Btw it will also work with your implementation. It's just that instead of storing nrw values in firstMin, secondMin and thirdMin - store the whole object and do comparisons like array[i].nrw < firstmin.nrw.
yes I think so too, I think there is just a little more to perfect it. but I think the way of your implementation is better than mine. thankyou
2

You have the array:

let array = [{student_id: 'asda123asd1', nrw: 70}, {student_id: 'asda123asd2', nrw: 80}, {student_id: 'asda123asd3', nrw: 50}, {student_id: 'asda123asd4', nrw: 50}, {student_id: 'asda123asd5', nrw: 100}];

First you need to sort it based on the desired property:

array.sort((a ,b) => a.nrw - b.nrw);

Then pick the first three element:

const lowestThree = array.slice(0,3);

1 Comment

yes already solved, thank you guys, you saved my day :)

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.