Javascript 将数组添加到对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10838746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:09:51  来源:igfitidea点击:

adding array to Object

javascriptjson

提问by MG1

I am trying to loop through Json twice: once for parent elements and then again for more detail. (This eventually will be exported to XML) In the meantime, how do I go about adding an array to an Object? My current code doesn't create XMLObjectDetail

我试图通过 Json 循环两次:一次用于父元素,然后再次用于更多细节。(这最终将导出到 XML) 同时,我如何将数组添加到对象?我当前的代码没有创建 XMLObjectDetail

 XMLObject = {};
 var XMLObjectDetail = [];

 $.each(data, function(index, element) {
        XMLObject.CardCode = element['CardCode'] 
        XMLObject.CardName = element['CardName'];
        console.log(XMLObject);

  $.each(element, function(key, value) { 
       XMLObject[[XMLObjectDetail.InvPayAmnt]] = value['InvPayAmnt']; 
      });
  });

回答by Bergi

After you clarified your request in the comments, the solution is easy:

在评论中澄清您的请求后,解决方案很简单:

var XMLObject = {};
var XMLObjectDetail = [];
XMLObject["XMLObjectDetail"] = XMLObjectDetail;

you may shorten it to

你可以把它缩短为

var XMLObjectDetail, XMLObject = {XMLObjectDetail: XMLObjectDetail = []};

However, I need to mention some serious flaws in your code:

但是,我需要提及您的代码中的一些严重缺陷:

XMLObject = {}; // no var keyword: the variable will be global
var XMLObjectDetail = [];

$.each(data, function(index, element) {
    // I don't know how your data object/array looks like, but your code will be 
    // executed many times
    // For each element, you will overwrite the properties
    XMLObject.CardCode = element['CardCode'] // missing semicolon
    XMLObject.CardName = element['CardName'];
    // so that the final XMLObject will only contain cardcode and -name of the last one
    // It will depend on your console whether you see different objects
    // or the same object reference all the time
    console.log(XMLObject);

    // This part is completey incomprehensible
    // you now loop over the properties of the current element, e.g. CardCode
    $.each(element, function(key, value) { 
        // and again you only overwrite the same property all the time
        XMLObject[[XMLObjectDetail.InvPayAmnt]] = value['InvPayAmnt'];
        // but wait: The property name you try to set is very, um, interesting.
        // first, XMLObjectDetail is still an (empty) Array and has 
        //  no 'InvPayAmnt' property - leads to a undefined
        // then, you build an Array with that [undefined] value as the only item
        // OMG, an array? JavaScript does only allow strings as property names,
        //  so the array will be converted to a string - resulting to the empty string ""
    });
});

回答by TheVillageIdiot

If you want to add objects for XMLObjectto array XMLObjectDetaildo like this:

如果要为XMLObject数组添加对象,XMLObjectDetail请执行以下操作:

var XMLObject, XMLObjectDetail = [];

 $.each(data, function(index, element) {
        XMLObject=new Object(); //or XMLObject = {};
        XMLObject.CardCode = element['CardCode'] 
        XMLObject.CardName = element['CardName'];
        console.log(XMLObject);

        XMLObjectDetail.push(XMLObject);//ADDED OBJECT TO ARRAY

      //DON'T KNOW WHAT ARE YOU TRYING TO DO HERE?
      $.each(element, function(key, value) { 
           XMLObjectDetail[[XMLObjectDetail.InvPayAmnt]] = value['InvPayAmnt']; 
      });
  });