我正在嘗試在我正在製作的angularjs應用程序中執行某些操作。我有很多電影,包括導演,製片人,電影組等等,我想創作一個只有導演的新陣列。Javascript/AngularJs - 如何用另一個數組中的對象填充數組?
我不知道如何用特定對象填充新數組形成另一個數組。 這裏是我的意思是一個快速簡單的例子:
this.products = [
{name: "apple", category: "fruit"},
{name: "iPhone", category: "tech"},
{name: "banana", category: "fruit"}
];
this.fruits = [];
for(i = 0; i < products.length; i++) {
if (products.category[i] == "fruit") {
/*code to add object to fruits array*/
}
}
請幫幫忙!謝謝!
[Array.prototype.push](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/推) – Itay
根據預期的結果可以使用衆多的javascript數組方法 – charlietfl
[Append to array]的可能重複(http://stackoverflow.com/questions/351409/appending-to-array) – rohithpr