BBBootstrap
Create
Snippets
Templates
Creators
Tools
Login
Register
BBbootstrap
Snippets
Templates
Tools
Blogs
Pricing
Register
Create
Codes Tagged javascript
Home
tags
javascript
Insert an item inside array at specific index in ES6 Javascript
<p>This example is to insert an item inside an array at a specific location using ES6 Javascript</p><pre class="ql-syntax" spellcheck="false">var arr = ["Hello","Hanko","Samsi","John","Nick"]; console.log(arr.join()); // Hello,Hanko,Samsi,John,Nick arr.splice(3, 1, "Marry"); console.log(arr.join()); // Hello,Hanko,Samsi,Marry,Nick </pre><p><br></p><p><br></p>
share
report
BBBootstrap Team
2 years ago
Sorting string in javascript ES6
<p>Sorting string in Javascript ES6. By default javascript, the sort method organizes elements alphabetically(i.e A-Z).</p><pre class="ql-syntax" spellcheck="false">const items = ['arms','red','green','orange','yellow']; //sort items const sortitems = items.sort(); console.log(sortitems); // ['arms', 'green', 'orange', 'red', 'yellow'] //reverse the items const reverseitems = sortitems.reverse(); console.log(reverseitems); // ['yellow', 'red', 'orange', 'green', 'arms'] </pre>
share
report
BBBootstrap Team
2 years ago
Sorting numbers in arrays in javascript ES6
<p>Sorting numbers in arrays in Javascript ES6. If we want to use the javascript <code>sort</code>method with numerical values, we need to pass it inside a callback arrow function, which will handle the comparison of values.</p><p><br></p><pre class="ql-syntax" spellcheck="false">const numbers = [1,5,3,2,19,8,4,18]; const sortnumbers = numbers.sort((a,b)=>a-b); console.log(sortnumbers); // [1, 2, 3, 4, 5, 8, 18, 19] </pre><p><br></p>
share
report
BBBootstrap Team
2 years ago
Sort String with numbers in javascript ES6
<p>Sort String with numbers in Javascript ES6. If we have a string with injected numeric items at the end, we can slice it and turn into a number in order to sort all the arrays.</p><p><br></p><pre class="ql-syntax" spellcheck="false">const alphanumbers = ['items 1', 'items 19', 'items 13', 'items 12', 'items 14']; const sortnumbers = alphanumbers.sort((a,b) => { return +a.slice(-1) - +b.slice(-1); }); console.log(sortnumbers); </pre>
share
report
BBBootstrap Team
2 years ago
Sorting with long numbers in javascript ES6
<p>Sorting with long numbers in Javascript ES6. If the numbers are bigger than 9 - we can use regular expressions to find them and sort elements of the array based on their values.</p><p><br></p><pre class="ql-syntax" spellcheck="false">const r = /\d+/; const alphanumbers = ['items 12','items 11','items 1302','items 999','items 144']; const sortnumbers = alphanumbers.sort((a,b) => { return a.match(r) - b.match(r); }); console.log(sortnumbers); // ['items 11', 'items 12', 'items 144', 'items 999', 'items 1302'] </pre><p><br></p><p><br></p><p><br></p>
share
report
BBBootstrap Team
2 years ago
Sorting objects in javascript ES6
<p>Sorting objects in javascript ES6. we can use the id of the Objects of Array in javascript es6.</p><p><br></p><pre class="ql-syntax" spellcheck="false">const alphanumbers = [ {id:8,name:'john'}, {id:4,name:'Kendey'}, {id:1,name:'Marry'}, {id:10,name:'Rodeo'}, {id:6,name:'Drive'} ]; const sortnumbers = alphanumbers.sort((a,b) => { return a.id - b.id; }); console.log(sortnumbers); </pre><p><br></p>
share
report
BBBootstrap Team
2 years ago
Flashing title notification in Javascript ES6.
<p>To flash title notification in Javascript ES6. we will use <code>setInterval</code> function.</p><p><br></p><pre class="ql-syntax" spellcheck="false">let showalert = false; const interval = setInterval(()=>{ document.title = showalert ? 'Chat Application' : '(1) New Message'; },1000); </pre>
share
report
Hitesh Chauhan
2 years ago
Remove duplicates from an array in javascript ES6
<p>The easiest way to remove the duplicates from an array is using a set function in javascript.</p><pre class="ql-syntax" spellcheck="false">const letters = ['a','a','b','c','c','c','d','e']; const unique = [...new Set(letters)]; console.log(unique); // a,b,c,d,e </pre><p><br></p>
share
report
BBBootstrap Team
2 years ago
Loop inside javascript ES6
<p>In javascript, there are many ways to iterate through the array. In this example, we will use <code>for...of</code> function. </p><p><br></p><pre class="ql-syntax" spellcheck="false">const alpha = ['ABC', 'EFG','IJK','LMN']; for (const alphas of alpha) { console.log(alpha); } </pre>
share
report
BBBootstrap Team
2 years ago
Filtering array data in javascript ES6
<p>Filtering array data in Javascript ES6 with following code</p><p><br></p><pre class="ql-syntax" spellcheck="false">const names = ['Hello', 'John', 'Teena', 'amber', 'meller', 'abuture','anna']; const filterednames = names.filter(name => name.length <= 5) console.log(filterednames); // ['Hello', 'John', 'Teena', 'amber', 'anna'] </pre><p><br></p>
share
report
Hitesh Chauhan
2 years ago
convert float value to number in javascript ES6
<p>simplest way to convert float value to number in Javascript ES6 with the following code</p><p><br></p><pre class="ql-syntax" spellcheck="false">let str = "12345.00"; str = str.substring(0, str.length - 3); console.log(str); // 12345 </pre>
share
report
Upasana Chauhan
2 years ago
Generate compact number in Javascript ES6
<pre class="ql-syntax" spellcheck="false">const formatter = Intl.NumberFormat('en', { notation:'compact' }); const numberk = formatter.format(1295); // 1.3K const number2 = formatter.format(1295000); // 1.3M const numberk3 = formatter.format(12950); // 13K const numberk4 = formatter.format(1295324423); // 1.3B </pre>
share
report
BBBootstrap Team
2 years ago
Format a date in Javascript.
<p>code to format a date in JavaScript.</p><p><br></p><pre class="ql-syntax" spellcheck="false">let objectDate = new Date(); let day = objectDate.getDate(); console.log(day); // 12 let currentmo = objectDate.getMonth(); month = currentmo + 1; console.log(month + 1); // 1 let year = objectDate.getFullYear(); console.log(year); // 2023 // Now you can set it accordingly // dd/mm/yyyy console.log(day + '/' + month + '/' + year); // mm/dd/yyyy console.log(month + '/' + day + '/' + year); </pre>
share
report
Ask SNB
1 year ago
Get the current url in Javascript.
<p>To get the current url use the following code.</p><p><br></p><pre class="ql-syntax" spellcheck="false">let url = new URL(window.location.href); console.log(url.href); </pre>
share
report
Ask SNB
1 year ago
Encode url in javascript.
<p>Best way to encode url in javascript is to use back tick(`) which is ES6 feature.</p><pre class="ql-syntax" spellcheck="false">let name = `bbb`; params = `name=${name}`; var myOtherUrl = `http://example.com/index.html?url=${encodeURIComponent(params)}`; console.log(myOtherUrl); </pre>
share
report
Ask SNB
1 year ago
remove elements that were fetched using querySelectorAll?
<p><span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81);">We can remove elements that were fetched using </span><code style="background-color: rgb(247, 247, 248); color: var(--tw-prose-code);">querySelectorAll</code><span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81);"> by iterating through the NodeList and calling the </span><code style="background-color: rgb(247, 247, 248); color: var(--tw-prose-code);">remove()</code><span style="background-color: rgb(247, 247, 248); color: rgb(55, 65, 81);"> method on each element. Here is an example:</span></p><pre class="ql-syntax" spellcheck="false">// Fetch the elements to remove const elements = document.querySelectorAll('.class-name'); // Iterate through the elements and remove them elements.forEach(element => { element.remove(); }); </pre>
share
report
Ask SNB
1 year ago
Delete all table rows except one with a given class in javascript
<p>One can remove all table rows except one with a given class by using querySelectorAll to fetch all rows, then iterating through the NodeList and checking if the class matches the desired class. If it does not, then remove the row.</p><p><br></p><pre class="ql-syntax" spellcheck="false">// Fetch all rows const rows = document.querySelectorAll('tr'); // Iterate through the rows and remove those that do not have the desired class const className = 'class-name'; rows.forEach(row => { if (!row.classList.contains(className)) { row.remove(); } }); </pre>
share
report
Ask SNB
1 year ago
sdsdssssssssssssssssssssssssssssssssssss
<p>ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss</p>
share
report
BBBootstrap Team
11 months ago
1
Add a code
Add Code
Remove ads
remove
Latest codes
view all
Latest Snippets
view all
BBbootstrap
Quick Links
Creators
BBBootstrap Tags
Snippets
About
Contact us
Tools
Online Editor
Color Palette
Gradient Colors
Quizes
Socials
Facebook
Twitter
Instagram
Youtube
© 2024 bbbootstrap. All rights reserved
Terms of Service
Privacy Policy