Click here to Skip to main content
15,441,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a result set result set which has rows(first row is heading) and columns (like dataset in .NET). I want to fetch all the values from one column out of from that result set. How can I achieve it. Below is my data look like.

{
"Col1": "abc",
"Col2": "pqr",
"Col3": "xyx",
"Col4": "aaa",
"Col5": "bbb",
"Col6": "ccc",
"Col7": "ddd"
},
{
"Col1": "aaa",
"Col2": "bbb",
"Col3": "ccc",
"Col4": "ddd",
"Col5": "eee",
"Col6": "fff",
"Col7": "ggg"
},

What I have tried:

JavaScript
if (r.length)//r is my result set {
          r.forEach(element => {
          const featureCode = element.column_name.filter();// Here I am trying to fetch column name value from r using for each loop
return featureCode; 
}
Posted
Updated 7-Feb-20 4:18am
v3
Comments
Richard Deeming 7-Feb-20 6:32am     CRLF
Since we can't see the shape of your object, we can't tell you how to query it. Click the green "Improve question" link and add an example of what your data looks like.
Telstra 7-Feb-20 7:59am     CRLF
@Richard, I have updated a question now.
Richard Deeming 7-Feb-20 8:18am     CRLF
No, that's not what your data looks like in Javascript. You need to show us the structure of the object graph stored in the variable r.
Telstra 7-Feb-20 10:12am     CRLF
Hi, My data in r will look like below. { "Col1": "abc", "Col2": "pqr", "Col3": "xyx", "Col4": "aaa", "Col5": "bbb", "Col6": "ccc", "Col7": "ddd" }, { "Col1": "aaa", "Col2": "bbb", "Col3": "ccc", "Col4": "ddd", "Col5": "eee", "Col6": "fff", "Col7": "ggg" },
ZurdoDev 7-Feb-20 7:41am    
Reply to the comment so that the user is notified, rather than adding a new comment.

1 solution

Based on your input data being an array of objects, with properties representing the columns, you can use the map method to extract the values of a single column:
JavaScript
const input = [
    {
        "Col1": "abc",
        "Col2": "pqr",
        "Col3": "xyx",
        "Col4": "aaa",
        "Col5": "bbb",
        "Col6": "ccc",
        "Col7": "ddd"
    },
    {
        "Col1": "aaa",
        "Col2": "bbb",
        "Col3": "ccc",
        "Col4": "ddd",
        "Col5": "eee",
        "Col6": "fff",
        "Col7": "ggg"
    }
];

const columnName = "Col3";

const output = input.map(x => x[columnName]);

/*
output === ["xyx", "ccc"]
*/
Demo - JSFiddle[^]
Array.prototype.map() - JavaScript | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900