数组中的JavaScript Foreach值为字符串

时间:2017-10-09 15:09:33

标签: javascript jquery arrays for-loop

寻找JS的一些帮助。我有一个数组阵列

var totalOrder = []
function CafeService(meal, starter, main, dessert){//takes value from text box input
var customerOrder = [meal, starter, main, dessert]

totalOrder.push(customerOrder ); 
}

这是正确填充的。可以有无限量的订单。我想在送到厨房之前查看订单。如何将数组中的每个索引放入字符串中,例如填充以下内容:

var mealTime;
var mealStarter;
var mealMain;
var mealDessert;

我希望我需要为每个人做一个吗?

foreach (customerOrder in totalOrder){
    var mealTime; //how to populate
    var mealStarter;
    var mealMain;
    var mealDessert;
}

通过一个客户订单编辑总订单:

var totalOrder = ["Breakfast","Coffee","Toast","Apple"]

4 个答案:

答案 0 :(得分:4)

您可以使用索引简单地影响这些变量:

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);
});

或者,如果您使用ES6语法,则可以使用destructuring assignment

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);
});

注意我.forEach使用了reasons代替for...in;经典for-loop也是一个有效的选项。您可以将for...of与ES6一起使用。

答案 1 :(得分:1)

我会改变你的结构,使用对象而不是数组中的数组。您甚至可以创建一个constructor function

io::Error

访问这些属性非常直观:

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)); 
}

答案 2 :(得分:1)

如果我可以提出一个小建议:将食物成分存储在物体而不是阵列中。然后你可以通过键访问它们。这将使您的代码更容易阅读,特别是如果它变得更大,因为您不需要记住每个索引如何对应于用餐的部分。 (即customerOrder [1]是初学者)

例如:

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.
}) 

答案 3 :(得分:0)

你可以拿一个物品来计算想要的物品。



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; }