【问题标题】:Array of Object with multidimension array具有多维数组的对象数组
【发布时间】:2020-10-11 02:54:18
【问题描述】:

这是我第一次使用 javascript。

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

这是我的代码


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
    ]
]

【问题讨论】:

  • 如果包含最终输出就很容易理解了。谢谢。
  • 最终输出应该是[table, time, menu: [name, price], [name, price], [name, price]]

标签: javascript arrays object multidimensional-array


【解决方案1】:
  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)

最终的输出应该是这样的:

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

【讨论】:

  • 我试过了,为什么菜单输出是[object],[object]
【解决方案2】:

如果您希望将menu 参数设置为array of array,则无需解构它,因为您以相同的格式将其传递给“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);

输出:


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2015-12-30
    • 1970-01-01
    • 2018-08-09
    • 2014-06-08
    相关资源
    最近更新 更多