Javascript 从对象创建逗号分隔的字符串
我有一个对象数组,试图从中生成逗号分隔的字符串。这是我的密码-Javascript 从对象创建逗号分隔的字符串,javascript,Javascript,我有一个对象数组,试图从中生成逗号分隔的字符串。这是我的密码- arrData = { "bl_number": "TCLPKGCTG1603200", "comodity": "GEN", "container": { "container_id": "CRSU9007907", "full_empty": true
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' + ',';
}
}
我正在行变量中保存逗号分隔的字符串。最终结果是
F/E,B/L No,FCL/LCL,COMMODITY,WGT
我的问题是,不知何故,行中的容器总是丢失。我需要这样的最终结果
CONTAINER,F/E,B/L No,FCL/LCL,COMMODITY,WGT
arrData=[…]
它应该是arrData={…}
行=…
到行+=…
任何地方arrData=[…]
它应该是arrData={…}
行=…
到行+=…
任何地方您可以这样做,因为对象中的for循环将逐个迭代属性,因此容器在结果字符串中的位置将取决于容器属性在对象数据中的位置
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);
您可以这样做,因为对象中的for循环将逐个迭代属性,因此容器在结果字符串中的位置将取决于容器属性在对象数据中的位置
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);
arrData=[{
“bl_编号”:“TCLPKGCTG1603200”,
“共态”:“GEN”,
“容器”:{
“集装箱id”:“CRSU9007907”,
“full_empty”:正确,
“类型”:1
},
“flc_lcl”:“FCL”,
“重量”:“25.0000”
}]
var行=”;
对于(i=0;i
arrData=[{
“bl_编号”:“TCLPKGCTG1603200”,
“共态”:“GEN”,
“容器”:{
“集装箱id”:“CRSU9007907”,
“full_empty”:正确,
“类型”:1
},
“flc_lcl”:“FCL”,
“重量”:“25.0000”
}]
var行=”;
对于(i=0;i }
您有一个数组,数组元素是对象。
您还需要迭代抛出数组元素属性
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' + ',';
}
}
}
您有一个数组,数组元素是对象。 您还需要迭代抛出数组元素属性
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' + ',';
}
}
}
他/她只是编辑了他的问题,我的回答在这一刻毫无意义!他/她只是编辑了他的问题,我的回答在这一刻毫无意义!