【问题标题】:How to push data in multi dimensional array format如何以多维数组格式推送数据
【发布时间】:2016-12-12 14:31:08
【问题描述】:

我有如下声明的对象数组,我想使用 for 循环将数据推送到此,假设我有两个主要产品,并且该产品有数量和代码,所以我无法提供数据我只需要知道如何推送在下面的数组中。

var hashtable = [];
var hashtable = [{
    recurrence_code:'',
    products:[{
                    code:'',
                    qty:''
                }],
    start_date:'',
    payment_type:'',
    payment_method:''
}];

for (var i = 0; i < 2 ; i++) {
    for (var j=0; j < 2; j++) {
        hashtable[i].recurrence_code = i;//dumy data
        hashtable[i].products[j].code =j;//dumy data
        hashtable[i].products[j].qty = j;//dumy data
        hashtable[i].start_date = i;//dumy data
        hashtable[i].payment_type = 'pay_as_you_go';
        hashtable[i].payment_method = 'sms2_pay_and_email';             
    }
}
console.log(hashtable);

但是上面的循环结构对我不起作用,我该如何推送数据?有什么帮助吗? 将上述数据推入数组后,它应该如下所示 输出

hashtable[0][recurrence_code] = 0;
 hashtable[0][products][0][code] = 0;
 hashtable[0][products][0][qty] = 0;
 hashtable[0][products][1][code] = 1;
 hashtable[0][products][1][qty] = 1;
 hashtable[0][start_date] = 0;
 hashtable[0][payment_type] = 'pay_as_you_go';
 hashtable[0][payment_method] = 'sms2_pay_and_email';

hashtable[1][recurrence_code] = 0;
hashtable[1][products][0][code] = 0;
hashtable[1][products][0][qty] = 0;
hashtable[1][products][1][code] = 1;
hashtable[1][products][1][qty] = 1;
hashtable[1][start_date] = 0;
hashtable[1][payment_type] = 'pay_as_you_go';
hashtable[1][payment_method] = 'sms2_pay_and_email';

这是尝试fiddle的小提琴

【问题讨论】:

  • 你想把产品2推送到订阅数组???
  • 我希望将 1 和 2 推送到订阅,以便我可以将该数组传递给我的 api,但我需要相同的格式。
  • 这看起来不是保存数据的正确方法,通常您需要使用对象数组之类的东西,并且每个对象都有一些属性,例如[{code: '', products:[],start_date:''...},{code: '', products:[],start_date:''...}]
  • 那么你可以创建但是我只需要获取该格式
  • 有人请告诉我如何在数组中循环该数据

标签: javascript arrays for-loop multidimensional-array


【解决方案1】:

所以问题是,subscriptions[i] 没有在循环内定义,所以我只是在循环内重新声明它:-) 感谢一位最好的朋友帮助我解决了这个问题。这是我得到的,它可能对某人有所帮助。干杯

var hashtable = [];

for (var i = 0; i < 2 ; i++) {
                if (!hashtable[i]) {
        hashtable[i] = [];
     }
    hashtable[i].start_date = i;//dummy data
    hashtable[i].recurrence_code = i;//dummy data
    hashtable[i].payment_type = 'pay_as_you_go';
    hashtable[i].payment_method = 'sms2_pay_and_email';
    for (var j=0; j < 2; j++) {
                if (!hashtable[i].products) {
                hashtable[i].products = [];
        }
       if (!hashtable[i].products[j]) {
                hashtable[i].products[j] = [];
        }
        hashtable[i].products[j].code =j;//dummy data
        hashtable[i].products[j].qty = j;//dummy data
    }
}
console.log(hashtable);

【讨论】:

    【解决方案2】:

    在对问题进行编辑后,这里有一个示例,说明如何实现您正在寻找的内容。

    问题和所需输出中存在一些错误,例如嵌套的 for 循环运行了 4 次,但预期输出只有 2 个元素。我认为这是由于对push 缺乏了解,并且我基于假设您在每个hashtable 项目中需要2 个产品来构建我的示例。

    //this is class that will build items for the hashtable var.
    function HashItem(recurrence_code, start_date, payment_type, payment_method) {
        //these are class vars - they will hold data for each instance of the class
        this.recurrence_code = recurrence_code;
        this.products = [];
        this.start_date = start_date;
        this.payment_type = payment_type;
        this.payment_method = payment_method;
    
        //this is a class function to add products
        this.add_product = function (prod) {
            //here we push product into the products array
            this.products.push(prod);
        }
    };
    
    //this is an other class. It will hold products.
    function Product(code, qty) {
        this.code = code;
        this.qty = qty;
    }
    
    //this is the hashtable you want to add data to. hashtable may not be appropriate name here...
    var hashtable = [];
    
    //here we create 2 items
    for (var i = 0; i < 2 ; i++) {
        //lets create a new item
        var new_item = new HashItem(i,i,'pay_as_you_go','sms2_pay_and_email');
        //we now create 2 products per item
        for (var j=0; j < 2; j++) {
            //lets create new product
            var new_product = new Product(j,j);
            //and add it to the hash item
            new_item.add_product(new_product);
        }
        //now, we need to push the new item into the hashtable.
        hashtable.push(new_item);
    }
    
    //JSON.stringify will convert out class to JSON string. Useful to send it to the back-end API if needed.
    //Note that angularjs can just take the object as is in $http calls and will convert it to JSON by itself
    console.log(JSON.stringify(hashtable));
    

    这将输出:

    [
        {
            "recurrence_code": 0,
            "products": [
                {
                    "code": 0,
                    "qty": 0
                },
                {
                    "code": 1,
                    "qty": 1
                }
            ],
            "start_date": 0,
            "payment_type": "pay_as_you_go",
            "payment_method": "sms2_pay_and_email"
        },
        {
            "recurrence_code": 1,
            "products": [
                {
                    "code": 0,
                    "qty": 0
                },
                {
                    "code": 1,
                    "qty": 1
                }
            ],
            "start_date": 1,
            "payment_type": "pay_as_you_go",
            "payment_method": "sms2_pay_and_email"
        }
    ]
    

    表单可以在hashtable 数组上重复,并且可以按原样发送到后端 API。

    让我知道这是否适合你。

    【讨论】:

    • 好极了,我会接受你的回答,但我也会更新我的解决方案......但真的很感谢像你这样的人,这里总是有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多