skip to Main Content

I have been searching for answers to this for awhile and can’t seem to see the obvious.

I’m attempting to return the value by index of a json string. Although the key appears to match, its returning -1 for index. I simply want the value, in this case “Ethan” for key username. I’ve verified that rows is valid json.

for (var r in rows) {
            table_body += '<tr>';
            for (var c in cols) {

                table_body += '<td>';
                //table_body += Object.keys(rows[r]);
                var idx = c;
                console.log(rows[r]);
                var o = rows[r]; //the object
                //var key = Object.keys(o)[idx];

                var key = cols[c].title;
                var index = Object.keys(cols).indexOf(key);
                console.log('key: '+key);
                console.log('index: ' + index);
                console.log('cols: ');
                console.log(cols[c]);
                console.log('c: ' + c);
                var value = o[index];
                table_body += value;
                table_body += '</td>';
            }
            table_body += '</tr>';
        }
        table_body += '</table>';

This is the output in the console showing “o”, which is the json, the key, which is username, and the index of that key, -1

enter image description here

EDIT added more logs…enter image description here

EDIT
This is the json for rows…

[{"id":136,"userName":"ethan","halfDays":1,"fullDays":11,"approvedDept":null,"approvedExec":true,"type":"vacation","createDate":"/Date(1332863557393)/","approvedDeptDate":null,"approvedExecDate":null,"leaveBegin":"/Date(1311570000000)/","leaveEnd":"/Date(1311570000000)/","notes":"Beginning Balance","overage":false,"department":null,"reminderSent":true},{"id":381,"userName":"ethan","halfDays":0,"fullDays":1,"approvedDept":null,"approvedExec":true,"type":"sick","createDate":"/Date(1336055792800)/","approvedDeptDate":null,"approvedExecDate":"/Date(1336021200000)/","leaveBegin":"/Date(1335502800000)/","leaveEnd":"/Date(1335502800000)/","notes":null,"overage":false,"department":"Marketing","reminderSent":true}]

and cols…

[{ "title":"userName" },{ "title":"leaveBegin" },{ "title":"leaveEnd" }]

3

Answers


  1. It looks like, you want to find index in array of objects. Use findIndex:

    let cols = [{ "title":"userName" },{ "title":"leaveBegin" },{ "title":"leaveEnd" }];
    let index = cols.findIndex(col => col.title == "leaveBegin");
    console.log(index);
    Login or Signup to reply.
  2. cols is an array of objects, Object.keys() just returns an array of the array indexes, not the values of the title properties of the objects.

    Use findIndex() to search for a matching object.

    var index = cols.findIndex(obj => obj.title == key)
    

    Alternatively, before the loops you could create an object that maps all the keys to their indexes, and use that.

    var indexes = {};
    cols.forEach(({title}, index) => indexes[title] = index);
    

    Then in the loop you would do

    var index = key in indexes ? indexes[key] : -1;
    
    Login or Signup to reply.
  3. To get the corresponding value to some field, consider the simpler example involving a dumb row:

    const row = { username: 'a', leaveBegin: 'b', leaveEnd: 'c'}
    

    consider the col:

    const col = {title: 'username'}
    

    you would get the value associated to username with

    row[col.title] //'a'
    

    now the array cols:

    const cols = [{ "title":"userName" },{ "title":"leaveBegin" },{ "title":"leaveEnd" }]
    const values = cols.map(({ title }) => row[title]) // ['a', 'b', 'c']
    

    Finally for each row:

    const rows = [{"id":136,"userName":"ethan","halfDays":1,"fullDays":11,"approvedDept":null,"approvedExec":true,"type":"vacation","createDate":"/Date(1332863557393)/","approvedDeptDate":null,"approvedExecDate":null,"leaveBegin":"/Date(1311570000000)/","leaveEnd":"/Date(1311570000000)/","notes":"Beginning Balance","overage":false,"department":null,"reminderSent":true},{"id":381,"userName":"ethan2","halfDays":0,"fullDays":1,"approvedDept":null,"approvedExec":true,"type":"sick","createDate":"/Date(1336055792800)/","approvedDeptDate":null,"approvedExecDate":"/Date(1336021200000)/","leaveBegin":"/Date(1335502800001)/","leaveEnd":"/Date(1335502800002)/","notes":null,"overage":false,"department":"Marketing","reminderSent":true}]
    const cols = [{ "title":"userName" },{ "title":"leaveBegin" },{ "title":"leaveEnd" }]
    rows.forEach(row => {
      cols.forEach(({ title }) => {
        console.log(title, row[title])
      })
    })
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search