2015-10-23 52 views
0

我有一個基於另一個代碼的動態更改數組,我試圖從相同的檢索特定數據。

這裏是一個動態產生的陣列的下$ scope.filtereditem樣品:

[{ 
    "active": true, 
    "createdAt": "2015-10-05T20:19:58.264Z", 
    "desc": "With arugula, smoked almonds & chipotle vinaigrette", 
    "flavors": [{ 
    "active": true, 
    "name": "Chocolate", 
    "price": 8 
    }, { 
    "active": false, 
    "name": "Strawberry", 
    "price": 8 
    }, { 
    "active": false, 
    "name": "Hazelnut", 
    "price": 8 
    }, { 
    "active": false, 
    "name": "Mint", 
    "price": 8 
    }], 
    "img": "https://signsrestaurant.ca/wp-content/uploads/2015/09/Watermelon-Quinoa-Jimaca-Salad.jpg", 
    "name": "Watermelon Quinoa Jicama Salad (<span class=\"vegan\">VE</span>, <span class=\"gfree\">GF</span>, <span class=\"dfree\">DF</span>)", 
    "objectId": "x1zpkWmvmP", 
    "price": 14, 
    "qty": 1, 
    "sides": [{ 
    "active": false, 
    "name": "Soup" 
    }, { 
    "active": false, 
    "name": "Salad" 
    }, { 
    "active": false, 
    "name": "Fries" 
    }], 
    "sizes": [{ 
    "active": false, 
    "name": "Small", 
    "price": 5 
    }, { 
    "active": true, 
    "name": "Medium", 
    "price": 10 
    }, { 
    "active": false, 
    "name": "Large", 
    "price": 15 
    }], 
    "type": "Soup", 
    "updatedAt": "2015-10-21T18:09:37.499Z" 
}, { 
    "active": true, 
    "createdAt": "2015-10-05T20:35:01.363Z", 
    "desc": "Buffalo mozzarella, tomato, marinated artichoke hearts, black olives, pesto & balsamic drizzle", 
    "flavors": [{ 
    "active": false, 
    "name": "Vanilla", 
    "price": 8 
    }, { 
    "active": false, 
    "name": "Almond", 
    "price": 8 
    }, { 
    "active": true, 
    "name": "Hazelnut", 
    "price": 8 
    }, { 
    "active": false, 
    "name": "Caramel", 
    "price": 8 
    }], 
    "img": "https://signsrestaurant.ca/wp-content/uploads/2015/09/Mediterranean-Salad.jpg", 
    "name": "Mediterranean Salad (<span class=\"veg\">V</span>, <span class=\"gfree\">GF</span>)", 
    "objectId": "nI5VSpdBUn", 
    "price": 15, 
    "qty": 2, 
    "sides": [{ 
    "active": false, 
    "name": "Soup" 
    }, { 
    "active": false, 
    "name": "Salad" 
    }, { 
    "active": false, 
    "name": "Fries" 
    }], 
    "sizes": [{ 
    "active": false, 
    "name": "Small", 
    "price": 0 
    }, { 
    "active": true, 
    "name": "Medium", 
    "price": 5 
    }, { 
    "active": false, 
    "name": "Large", 
    "price": 10 
    }], 
    "type": "Salad", 
    "updatedAt": "2015-10-21T18:09:33.422Z" 
}] 

這僅僅是一個示例並且陣列變化動態地基於另一代碼。我希望實現的是以範圍元素的形式檢索某些數據,我們將其命名爲$ scope.filteredmenu

這就是我堅持的地方。以下是我迄今爲止這個:

$scope.filteredmenu = function() { 
    var order = " "; 
    var side = " "; 
    angular.forEach($scope.filtereditem, function(item) { 
     var flavor = " "; 
     var size = " "; 
     order += item.name + "Qty: " + item.qty + " , "; 
     side += "Side: " + item.type + " , "; 
     angular.forEach(item.flavors, function(option) { 
     if (option && option.active) { 
      flavor += "Flavor: " + option.name + " , "; 
     } 
     }); 
     angular.forEach(item.sizes, function(option) { 
     if (option && option.active) { 
      size += "Size: " + option.name + " , "; 
     } 
     }); 
     menuorder += order + side + size + flavor; 
    }); 
    return menuorder; 
    }; 

基本上,我需要以這種格式輸出:

對於每個項目, 「item.name」數量:「item.qty」,邊:'item.type',Flavor(以活動爲準):option.name(在item.flavors)中,大小(以活動爲準):option.name(在item.sizes中)

最後,通過電子郵件API發送結果$ scope.filteredmenu。我不確定我做錯了什麼。任何與此代碼的幫助將不勝感激。

+0

不是最好的解決辦法,但你的代碼似乎除了側部確定。你面臨的任何特定問題? – Icycool

+0

感謝您的回覆!是的,當我通過Mandrill的電子郵件[API](https://mandrillapp.com/api/docs/messages.html)發送它時,我並不真正生成輸出。我發送$ scope.filteredmenu,但我沒有得到任何輸出,不知道爲什麼。我得到了所有其他功能,並完成了這些工作。 – user3839044

回答

1

有一些語法錯誤:

var output = function() { 
var order = " "; 
var side = " "; 
//Not defined 
var menuorder = ''; 
angular.forEach($scope.filtereditem, function(item) { 
    var flavor = " "; 
    var size = " "; 
    order += item.name + "Qty: " + item.qty + " , "; 
    //plus sign was missing 
    side += "Side: " + item.type + " , "; 
    angular.forEach(item.flavors, function(option) { 
    if (option && option.active) { 
     flavor += "Flavor: " + option.name + " , "; 
    } 
    }); 
    angular.forEach(item.sizes, function(option) { 
    if (option && option.active) { 
     size += "Size: " + option.name + " , "; 
    } 
    }); 
    menuorder += order + side + size + flavor; 
}); 
return menuorder; 
} 

$scope.filteredmenu = output(); 

這裏是工作JSFiddler https://jsfiddle.net/hefc5ewe/

+1

非常感謝!我現在工作:),天才! – user3839044