我有一个对象数组,声明如下,我想使用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';
这是小提琴尝试小提琴
编辑完问题后,这里有一个关于如何实现您想要的目标的示例。
问题和所需输出中存在一些错误,例如嵌套的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。
让我知道这是否适合你。
所以问题是,订阅[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);