How to get all values of an object


Joined
Mar 26, 2022
Messages
1
Reaction score
0
So I turned my MySQL database into a string which is being sent to the subpage "/customers" but... I need to sort the data. Currently, I have a script that shows each object's value using fetch (express.js obviously) like {id: 1, username: user, content: test} or {id: 2, username: user, content: test2 and the actual string that is displaying my "stringified" database looks like this [{"id": 1, "username": "user", "content: "test"},{"id": 2, "username": "user", "content: "test2"}], I think you get the point. But back to my main question, is there a way to sent all of the values for a certain value? eg. console.log(data.id) to print out 16 values. Thanks everyone, Alek
 
Ad

Advertisements

Joined
Mar 11, 2022
Messages
82
Reaction score
8
???
Seriously. There is no print_r or var dumb in js. But there is work arounds. What exatly is your goal?

Completely untested. But you may go from there

This function should return an array of values
Code:
function keyArrayBack(objectArray,key){
      var back=new Array();
      for(var i =0; i<objectArray.length; i++){
           back.push(objectArray[i][key]));
      }
      return back;
}

var oa=[{"id": 1, "username": "user", "content: "test"},{"id": 2, "username": "user", "content: "test2"}];
console.log(keyArrayBack(oa,'id'));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top