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

Getting output 4 undefined in javascript

This is code to only output the specified columns of the object. However, I am getting the output 4 times undefined.

const array1 = [{
  name: 'k',
  age: 5,
  sex: 'f'
}, {
  name: 'a',
  age: 2,
  sex: 'm'
}];

const result = function(col, arr) {
  for (let obj of col.values()) {
    for (let i = 0; i < arr.length; i++) {
      console.log(arr[i].obj);
    }
  }
}

result(['name', 'age'], array1);

>Solution :

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

You need to take the items of cols and change the accessor of arr.

const
    array1 = [{ name: 'k', age: 5, sex: 'f' }, { name: 'a', age: 2, sex: 'm' }],
    result = function(col, arr) {
        for (const key of col) {
            for (let i = 0; i < arr.length; i++) {
                console.log(arr[i][key]);
            }
        }
    };

result(['name', 'age'], array1);
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