Looking for some help in JS. I have an array of array's
寻求JS的一些帮助。我有一个数组的数组
var totalOrder = []
function CafeService(meal, starter, main, dessert){//takes value from text box input
var customerOrder = [meal, starter, main, dessert]
totalOrder.push(customerOrder );
}
This is populating correctly.There can be a unlimited amount of orders. I want to check through the order before sending to the kitchen. How can I put each index in the array into strings e.g. to populate the below:
这是正确填充的。可以有无限量的订单。我想在送到厨房之前查看订单。如何将数组中的每个索引放入字符串中,例如填充以下内容:
var mealTime;
var mealStarter;
var mealMain;
var mealDessert;
I expect I need to do this with a for each?
我希望我需要为每个人做一个吗?
foreach (customerOrder in totalOrder){
var mealTime; //how to populate
var mealStarter;
var mealMain;
var mealDessert;
}
EDIT Total Order with one customers order:
通过一个客户订单编辑总订单:
var totalOrder = ["Breakfast","Coffee","Toast","Apple"]
4 个解决方案
#1
4
You can simply affect those variables using their indexes :
您可以使用它们的索引简单地影响这些变量:
var totalOrder = [];
function CafeService(meal, starter, main, dessert) {
var customerOrder = [meal, starter, main, dessert];
totalOrder.push(customerOrder);
}
CafeService('1', 'Salad', 'Hamburger', 'Soda');
CafeService('1', 'Salad', 'Hamburger', 'Soda');
CafeService('1', 'Salad', 'Hamburger', 'Soda');
totalOrder.forEach(function (customerOrder) {
var mealTime = customerOrder[0];
var mealStarter = customerOrder[1];
var mealMain = customerOrder[2];
var mealDessert = customerOrder[3];
console.log(mealTime, mealStarter, mealMain, mealDessert);
});
Or, if you use ES6 syntax, you can use destructuring assignment :
或者,如果使用ES6语法,则可以使用解构赋值:
var totalOrder = [];
function CafeService(meal, starter, main, dessert) {
var customerOrder = [meal, starter, main, dessert];
totalOrder.push(customerOrder);
}
CafeService('1', 'Salad', 'Hamburger', 'Soda');
CafeService('1', 'Salad', 'Hamburger', 'Soda');
CafeService('1', 'Salad', 'Hamburger', 'Soda');
totalOrder.forEach(function (customerOrder) {
var [mealTime, mealStarter, mealMain, mealDessert] = customerOrder;
console.log(mealTime, mealStarter, mealMain, mealDessert);
});
Note I used .forEach
instead of for...in
for reasons; classic for-loop
is also a valid option. You could use for...of
with ES6.
注意我使用.forEach而不是......出于原因;经典的for-loop也是一个有效的选择。你可以用于...与ES6。
#2
1
I would change your structure to use objects rather than arrays in arrays. You could even create a constructor function so:
我会改变你的结构,使用对象而不是数组中的数组。你甚至可以创建一个构造函数,所以:
var totalOrder = [];
function Order(meal, starter, main, dessert)
{
this.meal = meal;
this.starter = starter;
this.main = main;
this.dessert = dessert;
}
function CafeService(meal, starter, main, dessert){//takes value from text box input
totalOrder.push(new Order(meal, starter, main, dessert));
}
accessing the properties is then very intuitive:
访问属性非常直观:
totalOrder.forEach(function (customerOrder) {
customerOrder.meal;
...
}
#3
1
If I may make a small suggestion: store the meal components in an object rather than an array. Then you can simply access them by key. This will make your code much easier to read, especially if it gets larger because you won't need to remember how each index corresponds to the part of the meal. (ie. customerOrder[1] is a starter)
如果我可以提出一个小建议:将食物成分存储在物体而不是阵列中。然后你可以通过键访问它们。这将使您的代码更容易阅读,特别是如果它变得更大,因为您不需要记住每个索引如何对应于用餐的部分。 (即customerOrder [1]是首发)
For example:
var totalOrder = []
function CafeService(meal, starter, main, dessert){//takes value from text box input
var customerOrder = {
meal:meal,
starter: starter,
main: main,
dessert: dessert
}
totalOrder.push(customerOrder );
}
CafeService("Dinner", "soup", "roast duck", "cake")
CafeService("Dinner", "Salad", "t-bone", "pudding")
CafeService("Breakfast", "Fruit", "Omlette", "muffin")
// Now just access by key and your code is self-documenting
totalOrder.forEach(function(order){
console.log(order.meal)
console.log(order.starter)
// etc.
})
#4
0
You could take an object for counting the wanted items.
你可以拿一个物体来计算想要的物品。
function service(meal, starter, main, dessert) {
totalOrder.push([meal, starter, main, dessert]);
}
function getCount() {
var count = { time: {}, starter: {}, main: {}, dessert: {} };
totalOrder.forEach(function (a) {
['time', 'starter', 'main', 'dessert'].forEach(function (item, i) {
if (a[i]) {
count[item][a[i]] = (count[item][a[i]] || 0) + 1;
}
});
});
return count;
}
var totalOrder = [];
service('breakfast', 'coffee', 'toast', '');
service('breakfast', 'coffee', 'toast', 'apple');
service('lunch', 'soup', 'pizza', 'ice');
service('lunch', 'soup', 'pizza', 'cookie');
service('dinner', 'toast', 'spaghetti', 'banana');
service('dinner', '', 'pizza', 'banana');
console.log(getCount());
.as-console-wrapper { max-height: 100% !important; top: 0; }
#1
4
You can simply affect those variables using their indexes :
您可以使用它们的索引简单地影响这些变量:
var totalOrder = [];
function CafeService(meal, starter, main, dessert) {
var customerOrder = [meal, starter, main, dessert];
totalOrder.push(customerOrder);
}
CafeService('1', 'Salad', 'Hamburger', 'Soda');
CafeService('1', 'Salad', 'Hamburger', 'Soda');
CafeService('1', 'Salad', 'Hamburger', 'Soda');
totalOrder.forEach(function (customerOrder) {
var mealTime = customerOrder[0];
var mealStarter = customerOrder[1];
var mealMain = customerOrder[2];
var mealDessert = customerOrder[3];
console.log(mealTime, mealStarter, mealMain, mealDessert);
});
Or, if you use ES6 syntax, you can use destructuring assignment :
或者,如果使用ES6语法,则可以使用解构赋值:
var totalOrder = [];
function CafeService(meal, starter, main, dessert) {
var customerOrder = [meal, starter, main, dessert];
totalOrder.push(customerOrder);
}
CafeService('1', 'Salad', 'Hamburger', 'Soda');
CafeService('1', 'Salad', 'Hamburger', 'Soda');
CafeService('1', 'Salad', 'Hamburger', 'Soda');
totalOrder.forEach(function (customerOrder) {
var [mealTime, mealStarter, mealMain, mealDessert] = customerOrder;
console.log(mealTime, mealStarter, mealMain, mealDessert);
});
Note I used .forEach
instead of for...in
for reasons; classic for-loop
is also a valid option. You could use for...of
with ES6.
注意我使用.forEach而不是......出于原因;经典的for-loop也是一个有效的选择。你可以用于...与ES6。
#2
1
I would change your structure to use objects rather than arrays in arrays. You could even create a constructor function so:
我会改变你的结构,使用对象而不是数组中的数组。你甚至可以创建一个构造函数,所以:
var totalOrder = [];
function Order(meal, starter, main, dessert)
{
this.meal = meal;
this.starter = starter;
this.main = main;
this.dessert = dessert;
}
function CafeService(meal, starter, main, dessert){//takes value from text box input
totalOrder.push(new Order(meal, starter, main, dessert));
}
accessing the properties is then very intuitive:
访问属性非常直观:
totalOrder.forEach(function (customerOrder) {
customerOrder.meal;
...
}
#3
1
If I may make a small suggestion: store the meal components in an object rather than an array. Then you can simply access them by key. This will make your code much easier to read, especially if it gets larger because you won't need to remember how each index corresponds to the part of the meal. (ie. customerOrder[1] is a starter)
如果我可以提出一个小建议:将食物成分存储在物体而不是阵列中。然后你可以通过键访问它们。这将使您的代码更容易阅读,特别是如果它变得更大,因为您不需要记住每个索引如何对应于用餐的部分。 (即customerOrder [1]是首发)
For example:
var totalOrder = []
function CafeService(meal, starter, main, dessert){//takes value from text box input
var customerOrder = {
meal:meal,
starter: starter,
main: main,
dessert: dessert
}
totalOrder.push(customerOrder );
}
CafeService("Dinner", "soup", "roast duck", "cake")
CafeService("Dinner", "Salad", "t-bone", "pudding")
CafeService("Breakfast", "Fruit", "Omlette", "muffin")
// Now just access by key and your code is self-documenting
totalOrder.forEach(function(order){
console.log(order.meal)
console.log(order.starter)
// etc.
})
#4
0
You could take an object for counting the wanted items.
你可以拿一个物体来计算想要的物品。
function service(meal, starter, main, dessert) {
totalOrder.push([meal, starter, main, dessert]);
}
function getCount() {
var count = { time: {}, starter: {}, main: {}, dessert: {} };
totalOrder.forEach(function (a) {
['time', 'starter', 'main', 'dessert'].forEach(function (item, i) {
if (a[i]) {
count[item][a[i]] = (count[item][a[i]] || 0) + 1;
}
});
});
return count;
}
var totalOrder = [];
service('breakfast', 'coffee', 'toast', '');
service('breakfast', 'coffee', 'toast', 'apple');
service('lunch', 'soup', 'pizza', 'ice');
service('lunch', 'soup', 'pizza', 'cookie');
service('dinner', 'toast', 'spaghetti', 'banana');
service('dinner', '', 'pizza', 'banana');
console.log(getCount());
.as-console-wrapper { max-height: 100% !important; top: 0; }