0

So here's the thing. I have 3 inputboxes that function as table cells(the header). I also run a script that, if one of the inputboxes' input has changed, fires a function that fetches data from my database, using an ajax request. A json is returned. Now I fetch objects from the data(json) returned, so I get something like {key1: '1', key2: 'myName', ...}. Now, the "problem" is that I don't know what the keynames are(I know the keynames but I don't want to use them). Yet I want all of the keys' values listed in separate arrays.

The script I have right now:

$.ajax({ //ajax request
    method: "POST",
    url: "../php/fetch.php",
    data: inputs,
    dataType: 'json',
    cache: false,
    success: function(data) {
        var j=0;
        var otp;
        for(var i = 0; i < data.length; i++) {
            var obj = data[i];
            console.log(obj.length);
            for(p=0; p<obj.length; p++){
                otp = obj[0];
                console.log(otp);
                j++;
            }
        }
    }
})

So I have written the code above but I get stuck at assigning the values. How do I get all keyvalues into separate arrays without knowing the column_names?


EDIT: I don't want to get the key values but I want to get the keyvalues their own array that contains the values from the database (of this particular key).


Example:

A json is returned [{key1: "value1", key2: "value2", /*more values*/...}, {key1: "value10", key2: "value20", /*more values*/...}] something like that. I create an obj that contains "a part of the json", like this {key1: "value1", key2: "value2", /*more values*/...}.

What I want:

I want to create an array from the obj that contains ["value 1", "value10"] in this case for key1. So for every key1 value in the json, get it and put it in an array.
I already tried obj.keys[0] but that returns 0 as a value (which doesn't match any value in my json at all)


Thank you very much for your help in advance! :)

Evoc

Ps. if anything is unclear feel free to ask, of course.

Evochrome
  • 1,205
  • 1
  • 7
  • 20
  • Not sure what you mean by "Yet I want all of the keys' values listed in separate arrays." Can you provide an example of what is the expected outcome? – VladFr Feb 23 '16 at 21:14
  • Possible duplicate of http://stackoverflow.com/questions/3068534/getting-javascript-object-key-list – VladFr Feb 23 '16 at 21:15
  • use `Object.keys()` to return array of key names – charlietfl Feb 23 '16 at 21:16
  • I have multiple keys (f.e. id, name, surname, etc.) and I want all of their values into a different array. So id would have 1 and 2 for example and I can just extract the values from that array – Evochrome Feb 23 '16 at 21:18

1 Answers1

2

You can have something like this:

var tmpObject = {}

data.forEach(function (row) {
  Object.keys(object).forEach(function (key) {
    if (tmpObject[key]) {
      tmpObject[key].push(row[key]);
    } else {
      tmpObject[key] = new Array(row[key]);
    }
  });
});

For a data object like this:

[{key1: "value1", key2: "value2"}, {key1: "value10", key2: "value20"}]

Should create another objet like this:

{
  key1: ["value1", "value10"],
  key2: ["value2", "value20"]
}
Eloy Pineda
  • 2,117
  • 17
  • 22