繁体   English   中英

Object 阵列与多维阵列

[英]Array of Object with multidimension array

这是我第一次使用 javascript。

我无法将人插入表中,任何人都可以帮助我吗? 所以,我想做一个简单的 function,我可以在其中插入一个人到表中,将名称和价格插入到带有二维数组的菜单中,并验证表是否已满,你不能插入到 object 的数组中。

这是我的代码


var d = new Date;
dformat = [d.getMonth() + 1,
    d.getDate(),
    d.getFullYear()
].join('/') + ' ' + [d.getHours(),
    d.getMinutes(),
    d.getSeconds()
].join(':');

function custDine([
    [name, price]
]) {
    this.table = new Array(5);
    this.date = dformat;
    this.menu = new Array(name, price);
}

function addCust([
    [name, price]
]) {
    customer.push(new custDine([
        [name, price]
    ]));
}

addCust([
    ["Pizza", 50000],
    ["max", 60000]
]);
console.log(customer);

the final output should be like this: 
[
    table,
    time,
    menu: [
        name,
        price
    ], [
        name,
        price
    ], [
        name,
        price
    ]
]
  let customer = []
    var d = new Date;
    dformat = [d.getMonth() + 1,
        d.getDate(),
        d.getFullYear()
    ].join('/') + ' ' + [d.getHours(),
        d.getMinutes(),
        d.getSeconds()
    ].join(':');

function custDine(menu) {
    this.table = new Array(5);
    this.date = dformat;
    this.menu = menu;
}

function addCust(items) {
    customer.push(new custDine(items));
}


addCust([
    {"item":"Pizza", "price":50000},
    {"item":"max", "price":60000}
]);
console.log(customer)

最终的 output 应该是这样的:

[
    table,
    time,
    menu: [
           {"item":"Pizza", "price":50000},
           {"item":"max", "price":60000}
    ]
]

如果您希望将菜单参数设置为array of array则不需要解构menu参数,因为您以相同的格式将其传递给“addCust”:

addCust([ ["Pizza", 50000], ["max", 60000] ]) <= Array of array

 var d = new Date; const dformat = [d.getMonth() + 1, d.getDate(), d.getFullYear() ].join('/') + ' ' + [d.getHours(), d.getMinutes(), d.getSeconds() ].join(':'); const customer = []; function custDine(menu) { this.table = new Array(5); // this.date = dformat; this.time = dformat; this.menu = menu; } function addCust(menu) { customer.push(new custDine(menu)); } addCust([ ["Pizza", 50000], ["max", 60000] ]); console.log(customer);

Output:


[
  {
    table,
    time,
    menu: [
      [
        name,
        price
      ],
      [
        name,
        price
      ]
    ]
  }
]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM