循环结果中的未定义对象

时间:2017-07-06 13:55:24

标签: javascript object

我在下面有我的代码,我注意到当我在我的html中显示它时,我的索引0 中得到[object Object]结果,这实际上应该给我{{ 1}}。
奇怪的是,在我的153826-00中,我得到了想要的结果 有人可以向我解释一下发生了什么吗?



console.log

var db = {"Root":{"DataPage":[{"Record":{"PPS_NUM":"3207987   ","PO_NUM":"153826-00                     ","OB_TYPE":"SOLK","SHIPMENT":"S258028   "}},{"Record":{"PPS_NUM":"3207500   ","PO_NUM":"10086398                      ","OB_TYPE":"SOLK","SHIPMENT":"S257877   "}},{"Record":{"PPS_NUM":"3207562   ","PO_NUM":"","OB_TYPE":"XFLK","SHIPMENT":"S257881X  "}},{"Record":{"PPS_NUM":"3198033   ","PO_NUM":"SAMPLES 6-14-17               ","OB_TYPE":"SOLK","SHIPMENT":"S255956   "}},{"Record":{"PPS_NUM":"3207443   ","PO_NUM":"491754-2279                   ","OB_TYPE":"SOLK","SHIPMENT":"S257873   "}},{"Record":{"PPS_NUM":"3204961   ","PO_NUM":"","OB_TYPE":"XFLK","SHIPMENT":"S257368   "}},{"Record":{"PPS_NUM":"3207635   ","PO_NUM":"","OB_TYPE":"XFLK","SHIPMENT":"S257904   "}},{"Record":{"PPS_NUM":"3205331   ","PO_NUM":"0188502                       ","OB_TYPE":"SOLK","SHIPMENT":"S257450   "}}]}};
var p = db.Root.DataPage;
for (var key in p) {
  if (p.hasOwnProperty(key)) {
     db+='<p>'+p[key].Record.PO_NUM+'</p>';
     $('#display').html(db);
      console.log(p[key].Record.PO_NUM);
 }
 }
$('#display').html(db);
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

那是因为你已经将db定义为你的第一个数组,所以当你+ =时它只是添加数据。 只需更改第二个数据库以获得输出即可。

&#13;
&#13;
var db = {"Root":{"DataPage":[{"Record":{"PPS_NUM":"3207987   ","PO_NUM":"153826-00                     ","OB_TYPE":"SOLK","SHIPMENT":"S258028   "}},{"Record":{"PPS_NUM":"3207500   ","PO_NUM":"10086398                      ","OB_TYPE":"SOLK","SHIPMENT":"S257877   "}},{"Record":{"PPS_NUM":"3207562   ","PO_NUM":"","OB_TYPE":"XFLK","SHIPMENT":"S257881X  "}},{"Record":{"PPS_NUM":"3198033   ","PO_NUM":"SAMPLES 6-14-17               ","OB_TYPE":"SOLK","SHIPMENT":"S255956   "}},{"Record":{"PPS_NUM":"3207443   ","PO_NUM":"491754-2279                   ","OB_TYPE":"SOLK","SHIPMENT":"S257873   "}},{"Record":{"PPS_NUM":"3204961   ","PO_NUM":"","OB_TYPE":"XFLK","SHIPMENT":"S257368   "}},{"Record":{"PPS_NUM":"3207635   ","PO_NUM":"","OB_TYPE":"XFLK","SHIPMENT":"S257904   "}},{"Record":{"PPS_NUM":"3205331   ","PO_NUM":"0188502                       ","OB_TYPE":"SOLK","SHIPMENT":"S257450   "}}]}};
var p = db.Root.DataPage;
var output = "";
for (var key in p) {
  if (p.hasOwnProperty(key)) {
     output+='<p>'+p[key].Record.PO_NUM+'</p>';
     $('#display').html(output);
      console.log(p[key].Record.PO_NUM);
 }
 }
$('#display').html(output);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="display"></p>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

db变量是一个对象,当您将字符串附加到此变量时,它会转换为字符串,然后您获得[object Object]
只需更改变量名称

即可

var db = {"Root":{"DataPage":[{"Record":{"PPS_NUM":"3207987   ","PO_NUM":"153826-00                     ","OB_TYPE":"SOLK","SHIPMENT":"S258028   "}},{"Record":{"PPS_NUM":"3207500   ","PO_NUM":"10086398                      ","OB_TYPE":"SOLK","SHIPMENT":"S257877   "}},{"Record":{"PPS_NUM":"3207562   ","PO_NUM":"","OB_TYPE":"XFLK","SHIPMENT":"S257881X  "}},{"Record":{"PPS_NUM":"3198033   ","PO_NUM":"SAMPLES 6-14-17               ","OB_TYPE":"SOLK","SHIPMENT":"S255956   "}},{"Record":{"PPS_NUM":"3207443   ","PO_NUM":"491754-2279                   ","OB_TYPE":"SOLK","SHIPMENT":"S257873   "}},{"Record":{"PPS_NUM":"3204961   ","PO_NUM":"","OB_TYPE":"XFLK","SHIPMENT":"S257368   "}},{"Record":{"PPS_NUM":"3207635   ","PO_NUM":"","OB_TYPE":"XFLK","SHIPMENT":"S257904   "}},{"Record":{"PPS_NUM":"3205331   ","PO_NUM":"0188502                       ","OB_TYPE":"SOLK","SHIPMENT":"S257450   "}}]}};
var p = db.Root.DataPage;
var htmlValue = "";
for (var key in p) {
  if (p.hasOwnProperty(key)) {
     htmlValue +='<p>'+p[key].Record.PO_NUM+'</p>';
     $('#display').html(htmlValue);
      console.log(p[key].Record.PO_NUM);
 }
 }
$('#display').html(htmlValue);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="display"></p>

答案 2 :(得分:0)

您需要将变量初始化为空字符串。它将它视为一个数组并带有+符号,它将添加到一个数组中。因此,您将获得[object Object]

var db = {
  "Root": {
    "DataPage": [{
      "Record": {
        "PPS_NUM": "3207987   ",
        "PO_NUM": "153826-00                     ",
        "OB_TYPE": "SOLK",
        "SHIPMENT": "S258028   "
      }
    }, {
      "Record": {
        "PPS_NUM": "3207500   ",
        "PO_NUM": "10086398                      ",
        "OB_TYPE": "SOLK",
        "SHIPMENT": "S257877   "
      }
    }, {
      "Record": {
        "PPS_NUM": "3207562   ",
        "PO_NUM": "",
        "OB_TYPE": "XFLK",
        "SHIPMENT": "S257881X  "
      }
    }, {
      "Record": {
        "PPS_NUM": "3198033   ",
        "PO_NUM": "SAMPLES 6-14-17               ",
        "OB_TYPE": "SOLK",
        "SHIPMENT": "S255956   "
      }
    }, {
      "Record": {
        "PPS_NUM": "3207443   ",
        "PO_NUM": "491754-2279                   ",
        "OB_TYPE": "SOLK",
        "SHIPMENT": "S257873   "
      }
    }, {
      "Record": {
        "PPS_NUM": "3204961   ",
        "PO_NUM": "",
        "OB_TYPE": "XFLK",
        "SHIPMENT": "S257368   "
      }
    }, {
      "Record": {
        "PPS_NUM": "3207635   ",
        "PO_NUM": "",
        "OB_TYPE": "XFLK",
        "SHIPMENT": "S257904   "
      }
    }, {
      "Record": {
        "PPS_NUM": "3205331   ",
        "PO_NUM": "0188502                       ",
        "OB_TYPE": "SOLK",
        "SHIPMENT": "S257450   "
      }
    }]
  }
};
var p = db.Root.DataPage;
var htmlToAppend = '';
for (var key in p) {
  if (p.hasOwnProperty(key)) {
    htmlToAppend += '<p>' + p[key].Record.PO_NUM + '</p>';
    console.log(p[key].Record.PO_NUM);
  }
}
$('#display').html(htmlToAppend);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="display"></p>