Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript: How to iterate only a certain part of a javascript object?

I have a javascript object that looks like this

const columns = {
    firstLabel: 'Hello',
    secondLabel: 'world',
    thirdLabel: 'I',
    fourthLabel: 'Am',
    fifthLabel: 'Paul',
};

I am trying to only get the values of the last three I, Am, Paul. I am doing something like this but this clearly doesn’t work as this will return everything. Is this possible to do with an object?

 for(const prop in columns) {
      if(Object.keys(columns).length > 2){
        console.log(`${columns[prop]}`);
      }
 }

Expected output is just:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I

Am

Here

>Solution :

Try with,

const lastThree = Object.values(columns).slice(-3);
console.log(lastThree);

The answer is:

[ 'I', 'Am', 'Paul' ]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading