从Object创建逗号分隔的字符串

时间:2016-07-16 10:33:53

标签: javascript

我有一个Object数组,我试图生成一个逗号分隔的字符串。这是我的代码 -

var token = window.localStorage.getItem('token');

 if (token) {
   $.ajaxSetup({
     headers: {
      'x-access-token': token
    }
  });
}

我在变量中保存逗号分隔的字符串。最终结果是

arrData = {
            "bl_number": "TCLPKGCTG1603200",
            "comodity": "GEN",
            "container": {
                           "container_id": "CRSU9007907",
                           "full_empty": true,
                           "type": 1
                         },
            "flc_lcl": "FCL",
            "weight": "25.0000"
          }

var row = "";

for (var index in arrData) {
    if(index == 'container'){
        for(var index_of_container_json in arrData[index]){
            if(index_of_container_json == 'container_id'){
                row = 'CONTAINER' + ',';
            }else if(index_of_container_json == 'full_empty'){
                row = 'F/E' + ',';
            }
        }
    }else if(index == 'bl_number'){
        row += 'B/L No' + ',';
    }else if(index == 'flc_lcl'){
        row += 'FCL/LCL' + ',';
    }else if(index == 'comodity'){
        row += 'COMMODITY' + ',';
    }else if(index == 'weight'){
        row += 'WGT' + ',';
    }
}

我的问题是,某种方式 CONTAINER 总是会从中丢失。我需要这样的最终结果

F/E,B/L No,FCL/LCL,COMMODITY,WGT

4 个答案:

答案 0 :(得分:3)

  1. 我在这里看到错误:arrData = [.. ]它应该是arrData = { ... }
  2. row = ...交换到row += ...无处不在

答案 1 :(得分:1)

你可以尝试这个。因为对象中的循环将逐个遍历属性,因此结果字符串中CONTAINER的位置将取决于对象arrData中容器属性的位置;

var arrData = {
            "bl_number": "TCLPKGCTG1603200",
            "comodity": "GEN",
            "container": {
                           "container_id": "CRSU9007907",
                           "full_empty": true,
                           "type": 1
                         },
            "flc_lcl": "FCL",
            "weight": "25.0000"
}

var row = "";
for (var index in arrData) {
    if(index == 'container'){
        for(var index_of_container_json in arrData[index]){
            if(index_of_container_json == 'container_id'){
                row += 'CONTAINER' + ',';
            }else if(index_of_container_json == 'full_empty'){
                row += 'F/E' + ',';
            }
        }
    }else if(index == 'bl_number'){
        row += 'B/L No' + ',';
    }else if(index == 'flc_lcl'){
        row += 'FCL/LCL' + ',';
    }else if(index == 'comodity'){
        row += 'COMMODITY' + ',';
    }else if(index == 'weight'){
        row += 'WGT' + ',';
    }
}

console.log(row);

答案 2 :(得分:1)

arrData = [{
  "bl_number": "TCLPKGCTG1603200",
  "comodity": "GEN",
  "container": {
    "container_id": "CRSU9007907",
    "full_empty": true,
    "type": 1
  },
  "flc_lcl": "FCL",
  "weight": "25.0000"
}]

var row = "";
for (i = 0; i < arrData.length; i += 1) {
  arrElem = arrData[i];
  row = "";
  for (var index in arrElem) {
    if (index == 'container') {
      str = '';
      for (var index_of_container_json in arrElem[index]) {
        if (index_of_container_json == 'container_id') {
          str += 'CONTAINER' + ',';
        } else if (index_of_container_json == 'full_empty') {
          str += 'F/E' + ',';
        }
      }
      row = str + row;
    } else if (index == 'bl_number') {
      row += 'B/L No' + ',';
    } else if (index == 'flc_lcl') {
      row += 'FCL/LCL' + ',';
    } else if (index == 'comodity') {
      row += 'COMMODITY' + ',';
    } else if (index == 'weight') {
      row += 'WGT' + ',';
    }
  }
  // remove the last comma.
  console.log(row.replace(/,\s*$/, ""));
}

答案 3 :(得分:0)

你有一个数组,你的数组元素是对象。 您还需要迭代抛出数组元素属性。

arrData.forEach(function(value){
         for(var index in value){
            if(index == 'container'){
                for(var index_of_container_json in arrData[index]){
                    if(index_of_container_json == 'container_id'){
                        row = 'CONTAINER' + ',';
                    }else if(index_of_container_json == 'full_empty'){
                        row = 'F/E' + ',';
                    }
                }
           }else if(index == 'bl_number'){
                row += 'B/L No' + ',';
           }else if(index == 'flc_lcl'){
                row += 'FCL/LCL' + ',';
           }else if(index == 'comodity'){
                row += 'COMMODITY' + ',';
           }else if(index == 'weight'){
                row += 'WGT' + ',';
           }
      }
  }