1

I am using datatables. I have a response that I need to show in datatable. Now, for the testing, I have taken a json and I am not able to extract values from it?

I checked there is jquery.map but I am not getting how to use it.

This is my json -

var json =  [{"_attr":{"col_Decision":{"_value":"Count"},"col1":{"_value":"2,698"},"col2":{"_value":"1,724"},"col3":{"_value":"4,422"},"col4":{"_value":"1,457"},"col5":{"_value":"1,047"},"col6":{"_value":"2,504"},"col7":{"_value":"1,241"},"col8":{"_value":677},"col9":{"_value":"1,918"}},"_text":[null]},{"_attr":{"col_Decision":{"_value":"Avg Days"},"col1":{"_value":4},"col2":{"_value":2.2},"col3":{"_value":3.3},"col4":{"_value":4},"col5":{"_value":1.9},"col6":{"_value":3.1},"col7":{"_value":4},"col8":{"_value":2.8},"col9":{"_value":3.5}},"_text":[null]},{"_attr":{"col_Decision":{"_value":"Goal Met %"},"col1":{"_value":84.2},"col2":{"_value":90.8},"col3":{"_value":86.8},"col4":{"_value":79.9},"col5":{"_value":91.1},"col6":{"_value":84.6},"col7":{"_value":89.4},"col8":{"_value":90.4},"col9":{"_value":89.7}},"_text":[null]}] 

Now I need the values of col_Decision, col1, col2, col3,...col9. There are 3 rows. So it will be 3 times in the response.

Its probably very easy but I am not getting it.

Failing code -

var itemvalue = arrayObj.data[0].itemlist[0].item;
                        const data = JSON.stringify(itemvalue);
                       // var myJsonString = JSON.stringify(itemvalue);
                        //console.log(myJsonString);
                        console.log(data);
                        //loadDataTable(data);

                            const rows = data.map( 
                            ({_attr}) => Object.keys(_attr).map ( key => _attr[key]._value )
                            ); 

                        console.log(rows);
3
  • Is this is what you want var result = json.map(function(obj) { return obj._attr; }); Commented Mar 25, 2017 at 10:31
  • no I want value for each column Commented Mar 25, 2017 at 10:38
  • 1
    Please read the usage description of the json tag. This is not about JSON. Commented Mar 25, 2017 at 11:45

4 Answers 4

2

Using JS, You might need to iterate each of the object's properties inside. In iterating, you might need this:

var aAssignColumnData = [];
for (var iIndex in json) {
    // We'll assign this variable to the [_attr] inside [json] object
    aAssignColumnData.push(json[iIndex]._attr);
}

// Now, [aAssignColumnData] has the object values containing the [_attr] object values.
for (var iColumnIndex in aAssignColumnData) {
    if (aAssignColumnData.hasOwnProperty(iColumnIndex) === true) {

       // e.g _attr[0] / _attr[1] / _attr[2]
       var oColumnNames = aAssignColumnData[iColumnIndex];

       // Then we'll get the number of properties inside the [json's _attr] values
       var iLength = Object.keys(oColumnNames).length; // e.g 10, since 3 of your [_attr] had 10 objects inside.

       // Then we'll iterate each of them using {for}
       for (var iCtr = 0; iCtr < iLength; iCtr++) {
           if (typeof oColumnNames['col' + iCtr] === 'object') {
               console.log(oColumnNames['col' + iCtr]['_value']);
           } else {
               // This is to retrieve the ['col_Decision'] property
               if (typeof oColumnNames['col_Decision'] === 'object') {
                  console.log(oColumnNames['col_Decision']['_value']);
               }
           }
        }
    }
}

Here's a jsfiddle for further reference: https://jsfiddle.net/8b99z0tL/

Hope this helps for your case.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe something like this? It uses map and es2015 lambas.

Array.from(Array(3)).map((a, i) => {
    return Array.from(Array(9)).map((b, j) => {
       return json[i]._attr["col"+(j+1)]._value;
    });
});

You could do it also with plain nested loops:

var result = [];
for(i=0; i<3; i++){
    var row = []
    for(j=1; j<=9; j++){
        row.push(json[i]._attr["col"+j])._value;
    }
    result.push(row)
}

3 Comments

for this do I need to add any library?
No, it's vanilla javascript.
there is one more column "col_Decision" whose value is needed
0

You can use this ES6 code:

data.map( ({_attr}) => Object.keys(_attr).map ( key => _attr[key]._value ) ); 

const data =  [{"_attr":{"col_Decision":{"_value":"Count"},"col1":{"_value":"2,698"},"col2":{"_value":"1,724"},"col3":{"_value":"4,422"},"col4":{"_value":"1,457"},"col5":{"_value":"1,047"},"col6":{"_value":"2,504"},"col7":{"_value":"1,241"},"col8":{"_value":677},"col9":{"_value":"1,918"}},"_text":[null]},{"_attr":{"col_Decision":{"_value":"Avg Days"},"col1":{"_value":4},"col2":{"_value":2.2},"col3":{"_value":3.3},"col4":{"_value":4},"col5":{"_value":1.9},"col6":{"_value":3.1},"col7":{"_value":4},"col8":{"_value":2.8},"col9":{"_value":3.5}},"_text":[null]},{"_attr":{"col_Decision":{"_value":"Goal Met %"},"col1":{"_value":84.2},"col2":{"_value":90.8},"col3":{"_value":86.8},"col4":{"_value":79.9},"col5":{"_value":91.1},"col6":{"_value":84.6},"col7":{"_value":89.4},"col8":{"_value":90.4},"col9":{"_value":89.7}},"_text":[null]}];

const rows = data.map( 
    ({_attr}) => Object.keys(_attr).map ( key => _attr[key]._value )
); 

console.log(rows);

NB: Don't call your variable json, since it is not: it is a JavaScript object. It would be JSON if it were a string.

6 Comments

when I using it is saying ReferenceError: data is not defined
Run the snippet please. Like I wrote, don't call your variable json, but something else, like data, which is what I did.
this error is coming... - data.map is not a function
Then your code is different from the snippet. Please check what is different. Or, post a snippet yourself (or jsfiddle) that reproduces that error. Does the snippet work for you, when you run it from within my answer?
So, can you provide a fiddle with your failing code?
|
0
<script>
var json =  [{"_attr":{"col_Decision":{"_value":"Count"},"col1":{"_value":"2,698"},"col2":{"_value":"1,724"},"col3":{"_value":"4,422"},"col4":{"_value":"1,457"},"col5":{"_value":"1,047"},"col6":{"_value":"2,504"},"col7":{"_value":"1,241"},"col8":{"_value":677},"col9":{"_value":"1,918"}},"_text":[null]},{"_attr":{"col_Decision":{"_value":"Avg Days"},"col1":{"_value":4},"col2":{"_value":2.2},"col3":{"_value":3.3},"col4":{"_value":4},"col5":{"_value":1.9},"col6":{"_value":3.1},"col7":{"_value":4},"col8":{"_value":2.8},"col9":{"_value":3.5}},"_text":[null]},{"_attr":{"col_Decision":{"_value":"Goal Met %"},"col1":{"_value":84.2},"col2":{"_value":90.8},"col3":{"_value":86.8},"col4":{"_value":79.9},"col5":{"_value":91.1},"col6":{"_value":84.6},"col7":{"_value":89.4},"col8":{"_value":90.4},"col9":{"_value":89.7}},"_text":[null]}]

for(var i=0;i<json.length;i++){
    var keyNames,valueObj;
    var attrObj=json[i]._attr;
    for (keyNames in attrObj) {
        valueObj=attrObj[keyNames];
        console.log(keyNames+" : "+valueObj._value);
    }
}
</script>

keyNames will give you name of the key and valueObj._value will give you value of that particular key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.