将json值映射到数组

时间:2017-03-25 10:26:19

标签: javascript jquery json

我正在使用数据表。我有一个响应,我需要在datatable中显示。现在,为了测试,我采用了json,我无法从中提取值?

我检查过jquery.map,但我没有得到如何使用它。

这是我的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]}] 

现在我需要col_Decision,col1,col2,col3,... col9的值。共有3行。所以它在回复中将是3次。

它可能很容易,但我没有得到它。

代码失败 -

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);

5 个答案:

答案 0 :(得分:2)

使用JS,您可能需要迭代内部的每个对象的属性。 在迭代中,您可能需要这样:

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']);
               }
           }
        }
    }
}

以下是jsfiddle以供进一步参考:https://jsfiddle.net/8b99z0tL/

希望这有助于您的情况。

答案 1 :(得分:1)

也许是这样的?它使用mapes2015 lambas。

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

你也可以使用普通的嵌套循环:

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)
}

答案 2 :(得分:0)

您可以使用此ES6代码:

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);

注意:不要调用变量 json ,因为它不是:它是一个JavaScript对象。如果它是一个字符串,它将是JSON。

答案 3 :(得分:0)

尝试以下方法:

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]}];
var res = json.map(function(value){
  return Object.keys(value._attr).map(function(e) {
    var obj = {};
    obj[e]=value._attr[e];
    return obj;
  });
});
console.log(res)

答案 4 :(得分: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会为您提供密钥的名称,valueObj._value会为您提供该特定密钥的值。