如何以多维数组格式推送数据

时间:2016-08-06 07:26:48

标签: javascript arrays for-loop multidimensional-array

我有以下声明的对象数组,我想使用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);

这是尝试fiddle

的小提琴

2 个答案:

答案 0 :(得分:0)

在对问题进行编辑之后,这里有一个关于如何实现所需内容的示例。

问题和所需输出中存在一些错误,例如嵌套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。

请告诉我这是否适合您。

答案 1 :(得分:0)

所以问题是,订阅[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);