includes method forEach method Do do something with each item of the array1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function getStockSymbols (stocks ) { var symbols = []; stocks.forEach(function (stock ) { symbols.push(stock.symbol); }); return symbols; } var symbols = getStockSymbols([ { symbol : "XFX" , price : 240.22 , volume : 23432 }, { symbol : "TNZ" , price : 332.19 , volume : 234 }, { symbol : "JXJ" , price : 120.22 , volume : 5323 }, ]); console .log(JSON .stringify(symbols));
map method 1 2 3 4 5 6 7 8 9 10 11 function getStockSymbols(stocks) { return stocks.map((stock) => stock.symbol); } var symbols = getStockSymbols([ { symbol: "XFX" , price: 240.22 , volume: 23432 }, { symbol: "TNZ" , price: 332.19 , volume: 234 }, { symbol: "JXJ" , price: 120.22 , volume: 5323 }, ]); console.log(JSON.stringify(symbols));
filter method create a new array containing only those items the passed the test.
1 2 3 4 5 6 7 8 9 10 11 12 function getStocksOver(stocks, minPrice) { return stocks.filter((stock) => stock.price >= minPrice); } var expensiveStocks = getStocksOver([ { symbol: "XFX" , price: 240.22 , volume: 23432 }, { symbol: "TNZ" , price: 332.19 , volume: 234 }, { symbol: "JXJ" , price: 120.22 , volume: 5323 }, ], 150.00 );console.log(JSON.stringify(expensiveStocks));
Chaining the Array map and filter methods Both map and filter do not modify the array. Instead they return a new array of the results. Because both map and filter return Arrays, we can chain these functions together to build complex array transformations with very little code1 2 3 4 5 6 7 8 9 10 11 12 13 14 var stocks = [ { symbol: "XFX" , price: 240.22 , volume: 23432 }, { symbol: "TNZ" , price: 332.19 , volume: 234 }, { symbol: "JXJ" , price: 120.22 , volume: 5323 }, ]; var filteredStockSymbols = stocks. filter(function(stock) { return stock.price >= 150.00 ; }). map(function(stock) { return stock.symbol; })
Initialize an Array with Values 1 2 var itemArray = new Array('item 1' , 'item 2' , 'items 3' );var itamArrayLit = ['item 1' , 'item 2' , 'item 3' ];
Créer un un tableau de taille précise et le remplir avec des objets1 2 Array.from({length: nb}, () => new StyledContainer()); // Créer une nouvelle instance pour chaque item du tableau new Array(9).fill(0); // Va remplir le tableau de 9 0, par coutre si c'est un objet, il va remplir le tableau 9 fois avec la même instance de l'objet. (donc si on supprime une instance dans le tableau, le tableau va etre vide)
ex http://jsbin.com/zusezinutu/1/edit?js,console
Convert an Array to a String 1 2 3 var numberArray = [1 , 2 , 3 , 4 , 5 , 5 , 6 , 7 , 8 , 9 ]; console.log( numberArray.toString() ); console.log( numberArray.join('') );
Add Items to an Array Within a Range 1 2 3 var numberArray = [1 ,2 ,3 ,4 ]; numberArray.fill(5 , 0 , 1 ); console.log(numberArray);
The Fill Method Will Add a Value Based on the Start and Ending Index Numbers
Append an Array to the End of Another Array 1 2 3 var numberArray = [1 ,2 ,3 ,4 ]; var numberArray2 = [5 ,6 ,7 ,8 ]; var resultArray = numberArray.concat(numberArray2);
Reverse the Order of the Items in an Array 1 2 3 var numberArray = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]; numberArray.reverse(); console.log(numberArray);