javascript实用库_实用Javascript阵列备忘单

javascript实用库

Javascript is undoubtedly widespread by now. Web is full of them. It is getting more accessible as well. Plenty of resources are available for anyone willing to learn. Tutorials, courses, forums, books, boot camps, all tried to make us able to make a working application from scratch.

毫无疑问,Java语言现在已经普及了。 网络充满了他们。 它也越来越容易访问。 任何愿意学习的人都可以获得大量资源。 教程,课程,论坛,书籍,新手训练营都试图使我们能够从头开始制作一个有效的应用程序。

But in my experience as mentor at Binar Academy, those jungle of knowledge is overwhelming for some. So I figured out that there should be condensed version of learning material. Furthermore, it should be both conceptual and practical. Javascript array is fine example. We use them a lot. So in this article, I assembled a list of common tasks of Javascript array manipulation. Hopefully, this can be helpful for Javascript developers.

但是以我在Binar Academy担任导师的经验来看,那些知识的丛林对某些人来说是压倒性的。 因此,我发现应该有精简版的学习资料。 此外,它应该是概念性的和实践性的。 JavaScript数组就是一个很好的例子。 我们经常使用它们。 因此,在本文中,我收集了Javascript数组操作的常见任务列表。 希望这对Javascript开发人员会有所帮助。

Please do mind that this article will not mention built-in Javascript array functions. You can find that here or here. I’m talking about finding maximum value of an array, deleting duplicates, etc. Practical things useful in many projects.

请注意,本文不会提及内置的Javascript数组函数。 您可以在这里这里找到。 我正在谈论寻找数组的最大值,删除重复项等。在许多项目中有用的实际操作。

As convention, I’m going to use regular function declaration with array(s) and some parameter. The output might be single value, an array, or arrays. Now, please enjoy my assortment of practical use cases using Javascript array.

按照惯例,我将使用带有数组和某些参数的常规函数​​声明。 输出可能是单个值,一个或多个数组。 现在,请享受我使用Javascript数组进行的各种实际使用案例。

Javascript数组的最大值/最小值 (Maximum/minimum value of Javascript array)

Given an array of numbers, find the maximum or minimum value of an array.

给定一个数字数组,请找到一个数组的最大值或最小值。

// get minimum value of an array
function minValueArray(arr) {
return Math.min.apply(null, arr);
}
// get maximum value of an array
function minValueArray(arr) {
return Math.max.apply(null, arr);
}

Javascript字符串数组中的最长元素 (Longest element in Javascript array of strings)

Given an array of strings, find the longest element (i.e most character count). There is possible case where there are more than one longest elements. We will separate algorithm for each case below.

给定一个字符串数组,找到最长的元素(即最多的字符数)。 可能存在一个以上的最长元素的情况。 我们将在下面针对每种情况分离算法。

// get the FIRST longest element in array of strings
function longestElement(arr) {
return arr.sort((a, b) => { return b.length - a.length; })[0];
}
// get ALL longest elements in array of strings
function longestElements(arr) {
let maxLength = arr.reduce((a,b) => a > b.length ? a : b.length, 0);
return arr.filter(element => element.length == maxLength);
}

在Javascript数组中选择随机元素 (Select Random Element in Javascript Array)

Given an array, return a random element in that array.

给定一个数组,返回该数组中的一个随机元素。

// get random index of an array
function getRandomIndex(array) {
return Math.floor(Math.random()*array.length))
}
// get random element of an array
function getRandomElement(array) {
return array[Math.floor(Math.random()*array.length))]
}

在字符串数组中查找与搜索词匹配的元素 (Finding elements that match search term in array of strings)

Given an array and search term, return new array containing elements that match the search term.

给定一个数组和搜索词,返回包含与搜索词匹配的元素的新数组。

// get the index of FIRST match
function findString(term, arr) {
for (var i=0; i<arr.length; i++) {
if (arr[i].match(term)) return i;
}
return -1;
}
// get the elements of ALL matches, return -1 if not found
function findAllString(term, arr) {
let result =[];
for (var i=0; i<arr.length; i++) {
if (arr[i].match(term)) {
result.push(arr[i]);
};
}
if (result.length > 0) {
return result;
} else {
return -1;
}
}

JavaScript数组参数验证 (Javascript array parameter validation)

Given a function with parameter, checks if it’s an array. Return true (or continue) if it is an array, otherwise stop.

给定带有参数的函数,请检查它是否为数组。 如果它是一个数组,则返回true(或继续),否则停止。

function stepsWithArrayValidation(arr) {
if (!Array.isArray(arr)) return -1;
return true; // or continue your code
}

删除重复项或获取Javascript数组中的不同元素 (Remove duplicates or get distinct elements in Javascript array)

Given an array, return new array with duplicated elements removed, leaving it as array of distinct values. Personally, I like the elegance of this approach.

给定一个数组,返回删除了重复元素的新数组,将其保留为不同值的数组。 就个人而言,我喜欢这种方法的优雅。

// return array of distinct elements, no more duplicates
function removeDuplicates(arr) {
return [...new Set(arr)]
}

There’s more to come. I’ll keep up with more use cases later. If you have feedback or ideas, please don’t hesitate to tell me.

还有更多。 我将在以后跟上更多用例。 如果您有任何意见或想法,请随时告诉我。

I am Yogi Saputro, a full stack developer and mentor at Binar Academy, a tech company focused on developing high quality engineering talents. I’m also passionate in data science and business. Check out my other stories to find out more.

我是 Yogi Saputro ,Binar Academy的全职开发人员和导师,Binar Academy是一家致力于培养高素质工程人才的技术公司。 我也对数据科学和业务充满热情。 查看其他故事,以了解更多信息。

翻译自: https://medium.com/@yogski/practical-use-cases-of-javascript-array-f1affcc61419

javascript实用库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值