python 构件二维数组_通过这四个构件块来升级您的javascript数组
python 構件二維數組
Arrays in JavaScript are something special, as they leverage the prototype feature of JS and provide a handy way to run functions directly from a reference to an array instance.
JavaScript中的數組很特別,因為它們利用了JS的原型功能,并提供了一種便捷的方法來直接從引用到數組實例運行函數。
I believe that using these four functions as much as possible within your code will not only make you more efficient, but also make you more popular on your team and generally more amazing at what you do.
我相信,在您的代碼中盡可能多地使用這四個功能不僅會提高您的效率,而且還會使您在團隊中更受歡迎,并且通常會使您的工作更加驚奇。
All four of these prototype functions accept a “callback” function, which is a pure function that accepts arguments and must return something expected back to the caller, whether it be a boolean liketrue or false, or a type like String, Object, Array, or Number.
所有這四個原型函數都接受一個“回調”函數,這是一個純函數,它接受參數并且必須將期望的值返回給調用者,無論它是boolean如true或false )還是類型(如String , Object , Array或Number 。
Let’s take a look at each of these useful Array functions and learn to make our code more concise!
讓我們看一下這些有用的Array函數,并學習使我們的代碼更簡潔 !
地圖— Array.prototype.map() (Map — Array.prototype.map())
Photo by Louis Hansel @shotsoflouis on Unsplash Louis Hansel的照片@shotsoflouis在Unsplash上map is a building block of most programming languages, and is definitely a wonderful Array function to start with.
map是大多數編程語言的構建塊,并且絕對是一個很棒的Array函數。
This line of code for instance, will loop through ANIMALS and create an array of Strings, with their name and animal type:
例如,以下代碼行將遍歷ANIMALS并創建Strings數組,其名稱和動物類型為:
You might already be familiar with map if you are using front-end frameworks like React or Vue where you quite often need to loop through an array of data to produce HTML:
如果您使用的是React或Vue等前端框架,那么您可能已經很熟悉map ,在這些框架中,您經常需要遍歷數據數組以生成HTML:
<ul>{ANIMALS.map((animal) =>
<li>
{animal.name} ({animal.type})
</li>
)}
</ul>
?
// produces:
<ul>
<li>Spot (dog)</li>
<li>Fluffy (cat)</li>
<li>Peppy (bird)</li>
<li>Lizzy (lizard)</li>
</ul>
The map function is all-around useful to convert array of something X into array of something Y.
map函數可用于將X數組轉換為Y數組 。
Reduce — Array.prototype.reduce() (Reduce — Array.prototype.reduce())
The reduce function is used like map but takes it even further.
reduce函數的用法與map一樣,但功能更進一步。
Instead of convert array of something X into array of something Y, reduce is designed to convert array of something X into something Y, meaning you are not limited to just making a new array.
reduce不是將X的數組轉換為Y的數組,而是設計了將X的數組轉換為Y的意思,這意味著您不僅限于制作新的數組 。
You can turn an array into Objects, Numbers, or Strings. Here we sum up the total weight of all of the ANIMALS:
您可以將數組轉換為Objects , Numbers或Strings 。 在這里,我們總結所有ANIMALS的總重量:
reduce is such a powerful function on arrays, that it is versatile enough to produce the same effect as a map does. Check out this reduce version of a map function:
reduce是數組上如此強大的功能,它具有足夠的通用性,可以產生與map相同的效果。 看看這個map函數的reduce版本:
篩選器— Array.prototype.filter() (Filter — Array.prototype.filter())
Photo by Caleb Dow on Unsplash Caleb Dow在Unsplash上拍攝的照片filter works just as it sounds, and only retrieves items from an array that produce a truthy value from your callback function. Here we look for ANIMALS that have soft fur or feathers:
filter工作原理與聽起來一樣,僅從可從回調函數產生真實值的數組中檢索項目。 在這里,我們尋找具有柔軟皮毛或羽毛的ANIMALS :
Here is another example, using a mathematical comparison. As long as the function returns a truthy/falsy value it works as expected. Here we only select ANIMALS that weigh less than 10lbs:
這是另一個使用數學比較的例子。 只要函數返回真實/虛假值,它就會按預期工作。 在這里,我們只選擇重量小于10磅的ANIMALS :
查找— Array.prototype.find() (Find — Array.prototype.find())
Finally, we have find, which helps you search through an array using a callback. This is extremely useful as it provides an interface to search through an array that also works with a sister function called findIndex.
最后,我們find ,它可以幫助您使用callback遍歷數組。 這非常有用,因為它提供了一個搜索數組的接口,該數組也可以與名為findIndex的姊妹函數findIndex 。
As long as your function returns a truthy value, the first truthy value returned will exit the find search and return back that row from the array.
只要您的函數返回真實值,返回的第一個真實值將退出find搜索并從數組中返回該行。
In this example, we find a dog named Spot:
在此示例中,我們find了一只名為Spot的狗:
組合數組函數 (Combining Array Functions)
Photo by Ali Yahya on Unsplash Ali Yahya在Unsplash上拍攝的照片Now that you have a handle on these four building block functions, we can combine them all together:
現在您已經掌握了這四個構建基塊函數,我們可以將它們全部組合在一起:
Above, we first map over our ANIMALS and create a new object of their weight and type, also taking this opportunity to feed each one. We feed them and they might gain about 1% of their body weight after eating - this is just a guess.
上面,我們首先在ANIMALS map ,并創建一個重量和類型相同的新對象,并借此機會喂食每個ANIMALS 。 我們喂養他們,他們進食后可能會增加體重的1%-這只是一個猜測。
Then we filter out only animals that have fur or feathers, notice we did this after feeding, since we don’t want to starve our lizard Lizzy 🦎:
然后我們只過濾出有毛皮或羽毛的動物,請注意,喂食后我們這樣做了,因為我們不想餓死蜥蜴Lizzy🦎:
.filter((animal) => animal.type !== 'lizard') // only soft
.filter((animal) => animal.type !== 'lizard') // only soft
Then we filter out animals over a certain weight limit, which removes the dog’s entry 🐕:
然后我們篩選出超過一定重量限制的動物,從而刪除狗的入口🐕:
.filter((animal) => animal.weight < 10) // only light (after eating)
.filter((animal) => animal.weight < 10) // only light (after eating)
Finally, we reduce the remaining animal rows into a single value, a number representing their weight 🐈🐦:
最后,我們將剩余的動物行簡化為一個值,該數字代表它們的權重🐈🐦:
.reduce((weightSum, animal) => weightSum + animal.weight, 0); // get total weight
.reduce((weightSum, animal) => weightSum + animal.weight, 0); // get total weight
All of this is easily and flawlessly chained together thanks to Array prototypes and JavaScript magic.
借助Array原型和JavaScript魔術,所有這些都可以輕松,完美地鏈接在一起。
翻譯自: https://levelup.gitconnected.com/level-up-your-javascript-arrays-with-these-four-building-block-functions-d01ca0971bcf
python 構件二維數組
總結
以上是生活随笔為你收集整理的python 构件二维数组_通过这四个构件块来升级您的javascript数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python pandas获取行号_py
- 下一篇: 大一到大二的总结与感想