Some useful techniques for array operation using javascript
There are few development tips for Javascript array operation, these tips will reduce your development time.
1. Initialize an array with default value
If you are working with fixed length of array and you need to initialize it with fixed value. We have traditional way of initialzation but also we have alterative and it is a shortcut method.
Initialize one dimensional array If you want to intialize one dimensional array of specified length and apply the default values.
const oneArray = Array(6).fill('xxx');
//["xxx", "xxx", "xxx", "xxx", "xxx", "xxx"]
Initialize Multi-dimentsional array
If you want to intialize multi-dimensional array of specified length and apply the default values.
const multiArray = Array(6).fill(0).map(() => Array(5).fill('xxx'));
// [["xxx", "xxx", "xxx", "xxx", "xxx"], ["xxx", "xxx", "xxx", "xxx", "xxx"], ["xxx", "xxx", "xxx", "xxx", "xxx"], ["xxx", "xxx", "xxx", "xxx", "xxx"], ["xxx", "xxx", "xxx", "xxx", "xxx"], ["xxx", "xxx", "xxx", "xxx", "xxx"]]
2. Find Sum, Maximum and Minimum of an array.
It is a very tricky and this is a common question in most of interviews. To solve this we can use javascript standard function "reduce()".
The reduce() method executes a reducer function for array element.
const sampleArray = [51,14,17,28,39,32];
Sum
sampleArray.reduce((a,b) => a+b);
//Result: 181
Finding Maximum
sampleArray.reduce((a,b) => a > b ? a : b); Math.max(...sampleArray)
//Result: 51
Finding Minimum
sampleArray.reduce((a,b) => a < b ? a : b); Math.min(...sampleArray)
//Result: 14
3. Empty an array
If you want to clear the values in the array variable then set the length equal to Zero.
let array = ["J", "A", "V", "A", "S", "C", "R", "I", "P", "T"]
array.length = 0
console.log(array) // []
4. Split the word into an Array
We can split the word by given identifier, this is can be achieved by using split()
Eg :- 1
let array = "JAVASCRIPT"
console.log(array.split(""))
Result //["J", "A", "V", "A", "S", "C", "R", "I", "P", "T"]
Eg :- 2
let array = "JAVA-SCRIPT"
console.log(array.split("-"))
Result //["JAVA", "SCRIPT"]
the above are the two example of split function on different scenario.
5. Combine two or more arrays
If you want to Merge or Combine two or more arrays then use the Spread Operator
or Concat()
using Spread or Extension operator
const start = [1, 2]
const end = [5, 6, 7]
const result = [9, ...start, ...end, 8] // [9, 1, 2, 5, 6, 7 , 8]
using concat method
const start = [1, 2, 3, 4]
const end = [5, 6, 7]
start.concat(end); // [1, 2, 3, 4, 5, 6, 7]
More Stories
Cross-Origin Resource Sharing (CORS) is a security feature that lets a web page from one domain request resources from a different domain
SVG elements will not add the accessibility atttributes by default, so that will fail to describe by itself, and the NVDA and other screen reader required these attributes to work.
Despite being acquainted with git, many developers struggle to resolve these conflicts due to a lack of understanding of how to pull the conflict details into their local machines.
Firebase Authentication is one of its gems, allowing you to add user authentication effortlessly. It's secure, reliable, and comes with Google's seal of approval.
Why am I getting an auth/invalid-api-key error when setting the Firebase values in the environment variable on NextJS ?
Enzyme Internal Error: Enzyme expects an adapter to be configured, but found none.
Easist way of downloading the SVG file as PNG file is done using javascript snippet
To keep the code is safe and distrubuted between multiple resources that been achieved with the help of GIT
The importance of the http response headers are highly needed to protect the websites from hackers. If you poorly managed the response header then one day the website will be compromise to the hacker.
An HTTP header is a response by a web server to a browser that is trying to access a web page.
Application Insights is an feature of Azure Monitor and it provides application performance monitoring features. APM tools are very useful to analyse applications from development, testing and production release.
A lazy function lets you defer the loading of a components code until it is rendered for the first time. Before, it will remain in the bundle. So that we can reduce the load of the application.
We covered most asked questions for Javascript interview and their answers
we are displaying these emojis with the help of ASCII code and it is not that easy to remember because its a mix of numeric and special characters.
ES6 or the ECMAScript 2015 is the major edition of ECMAScript language, it introduced several new features which are very special to the developers
what are the new features among the various versions of ECMA script and what is difference
We can squash the number of commits from a git branch
Your focus-trap must have at least one container with at least one tabbable node in it at all times, when using dialog or modal in ReactJS or other front-end framework
Writing test cases for modal popup in jest
Cannot read property location of undefined, this is an common test cases error in react jest while using useLocation hook in your react component
There is a common problem when parsing the markdown file the ID attribute is missing in the element, here we found a solution to fix/overcome
It is basicall demonstrating how to find the fibanocci, amstrong, prime numbers and pyramid pattern using javascript.
Markdown is a lightweight markup language that you can use to add formatting elements to plaintext text documents.
For every website the Sitemap will be playing important role for SEO performance. In Ecommerce and other consumer websites also SEO have important role for developing their revenue.
This question is very usual, to get solve this issue by using the browser property user agent to check whether the device type.
What are the possible ways to create objects in JavaScript, The traditional way to create an empty object is using the Object constructor. But currently this approach is not recommended.