Related
This question already has answers here:
Split array into chunks
(73 answers)
Closed 5 months ago.
Like if there are 20 elements in an array what is the quickest way to filter them out by tens?
Like for example elements, 0-9 and then 10-21?
I think, what you mean is splitting the array to chunks each chunk with size equal to ten.
function chunkArray(array, size) {
let result = []
for (let i = 0; i < array.length; i += size) {
let chunk = array.slice(i, i + size)
result.push(chunk)
}
return result
}
let arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't'];
let arrPairs = chunkArray(arr, 10);
console.log(arrPairs);
You can try:
array.reduce((accum, current) => {
if (accum[accum.length - 1].length === 10) {
accum.push([]);
}
accum[accum.length - 1].push(current);
return accum;
},[[]])
♦ pattern:
e,b,c,d,i,f,g,h,o,j,k,l,m,n,u,p,q,r,s,t,a,v,w,x,y,z
I want to sort my words from arr alphabetically by 1st letter and then by 2nd letter of a similar word matched by 1st letter according to my given pattern.
['aobcdh', 'aibcdh', 'aabcdh', 'aacbdh', 'cfghjd', 'cighjd']
♦ output should be:
['aibcdh', 'aobcdh', 'aabcdh', 'aacbdh', 'cighjd', 'cfghjd' ]
♦ or:
aibcdh
aobcdh
aabcdh
aacbdh
cighjd
cfghjd
My Code here:
let pattern = ['e', 'b', 'c', 'd', 'i', 'f', 'g', 'h', 'o', 'j', 'k', 'l', 'm', 'n', 'u', 'p', 'q', 'r', 's', 't', 'a', 'v', 'w', 'x', 'y', 'z']
let arr = ['aobcdh', 'aibcdh', 'aabcdh', 'aacbdh', 'cfghjd', 'cighjd']
let arrSorted = arr.sort() //Natural sorting
console.log(arrSorted)
// output in array
const newArr = arrSorted.sort((a, b) => pattern.indexOf(a[1]) - pattern.indexOf(b[1]))
console.log(newArr) //Sorted by its 2nd character with given pattern
// single output without array
for (let i = 0; i < pattern.length; i++) {
for (let j = 0; j < arrSorted.length; j++) {
if (pattern[i] === arrSorted[j][1]) {
console.log(arrSorted[j]) //Sorted by its 2nd character with given pattern
}
}
}
You could sort the first by string and the second by custom value.
const
pattern = 'ebcdifghojklmnupqrstavwxyz',
array = ['aobcdh', 'aibcdh', 'aabcdh', 'aacbdh', 'cfghjd', 'cighjd'],
order = Object.fromEntries(Array.from(pattern, (l, i) => [l, i + 1]));
array.sort((a, b) =>
a[0].localeCompare(b[0]) ||
order[a[1]] - order[b[1]]
);
console.log(array);
I have an array of the entire alphabet from a to z. I also have an input field. I want to be able to find the index of each character from the input field in the alphabet array but my function doesn't work. I've tried storing the text from the input field into an array, and I've tried using a named function for it as well but neither worked.
<input type="text" id="plaintext" placeholder="Plaintext">
<div id="start"><div id="start_text">Start</div></div>
let plaintext = document.getElementById("plaintext");
let alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let startB = document.getElementById("start");
let plainParser = [];
startB.addEventListener('click', () => {
for(let i=0; i < alph.length; i++){
console.log(alph.findIndex( () => plainParser.push(plaintext.value.split(''))));
};
});
A shortcut without needing the array is use the charCode of each character.
a starts at 97
const str = 'abc';
for(let s of str){
console.log(s.charCodeAt(0) - 97);
}
I want to … find the index of each character from the input field in the alphabet array
Then instead of looping from 0 to 25:
for(let i=0; i < alph.length; i++){
you should loop over every character from the input:
for (let c of plaintext.value) {
I want to … find the index of each character from the input field in the alphabet array
You have the character, so find the index:
alph.indexOf(c)
v‘là.
let plaintext = document.getElementById("plaintext");
let alph = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let startB = document.getElementById("start");
startB.addEventListener('click', () => {
for (let c of plaintext.value) {
console.log(alph.indexOf(c));
}
});
Here's a slightly refactored version of what I think you are looking for:
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const result = document.querySelector(".result");
const plaintext = document.getElementById("plaintext");
const startB = document.querySelector(".start");
startB.addEventListener('click', () => {
const source = plaintext.value;
result.innerText = '';
if (!source) return;
const indices = [];
for (let char of source) {
indices.push(alphabet.indexOf(char));
}
result.innerText = indices.join(', ');
});
<input type="text" id="plaintext" placeholder="Plaintext">
<button class="start">Start</button>
<div class="result" style="font-family: monospace;"></div>
I'm trying to build a simple "cipher" to assign the letters in a string the value of a reversed alphabet (e.g. a=z b=y, etc..) I built a for statement that seems to work until the last call and instead of giving the reversed value instead gives the original value.
I commented out each line in the for statement and then worked through them one at a time to check their returns. The second to last statement (c = rev.indexOf(c);) give the value of 25, 24, 23 (as expected) so I don't understand why when it looks them up in the "letters" matrix it gives a return of a, b, c instead of z, y, x.
var letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'y', 'x', 'z'];
var rev = letters.reverse();
var stringy = function(n){
ns = n.split("");
for (i = 0; i < n.length; i++){
c = ns[i];
c = rev.indexOf(c);
c = letters[c];
console.log(c);
}
}
stringy("abc");
letters.reverse() doesn't just return the reversed array. It reverses the array in place.
If you check the contents of letters after letters.reverse() you'll see that the order has been reversed.
You can use .slice() to make a copy of the array, which you can then reverse:
var rev = letters.slice().reverse();
As an addition to zzzzBov information to the Array.reverse() method, I thought I would add an answer that doesn't require reversing your letters. There is no need to keep 2 variables just to reverse them based on the index, just find the index in the letters array and search from the end of the letters array back.
I added a slightly modified version of your code, to show how you could make it yourself a bit easier and added some code comments where I thought appropriate
// make your debugging easier
'use strict';
var letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'y', 'x', 'z'];
var getReversedCypher = function( text, sequence, unmatched ) {
if (!text) {
return '';
}
var ns = text.split(''),
cypher = [],
lastLetterIndex = sequence.length - 1;
// use strict adds forEach on arrays
ns.forEach(function(char) {
var index = sequence.indexOf(char);
if (index < 0) {
// couldn't find a match, you could throw an error, I chose to add an extra character
cypher.push( unmatched || '-' );
} else {
// no need to use a reversed lookup, just search the table from the end
cypher.push( sequence[lastLetterIndex-index] );
}
});
return cypher.join('');
};
// make some utility methods to still use your simple function
var encode = function( text ) {
return getReversedCypher( text, letters, '-' );
};
var decode = function( text ) {
return getReversedCypher( text, [].concat(letters).reverse(), ' ' );
};
var encoded = encode("this is the sound of speech"), decoded = decode(encoded);
console.log('cypher: ' + encoded);
console.log('decoded: ' + decoded);
I would like to get rid of special characters in a string by comparing each of it's character to a character in an array and replacing it with a matching one. The function below does not throw any errors but keeps returning the string unchanged
var name = "przykład";
// the characters i'm looking for in a string:
var charList = ["Ą","ą","Ć","ć","Ę","ę","Ł","ł","Ó","ó","Ś","ś","Ź","ź","Ż","ź"];
// the characters i'd like to replace them with:
var replaceList = ["A","a","C","c","E","e","L","l","O","o","S","s","Z","z","Z","z"];
var limit = name.length;
for (i = 0; i < limit; i++){
for(var j in charList){
name.charAt(i) === charList[j] ? name.replace(name.charAt(i), replaceList[j]) : "";
}
}
return name;
I know this question will be most likely closed as "too localized" and it's propably a stupid and easy mistake i've made but still I would really appreciate any help with this
Usually, the result of the replace function is returned as a new String object in most of the programming languages. You should change your code to this:
if (name.charAt(i) === charList[j])
name = name.replace(name.charAt(i), replaceList[j]);
Also, since the replace function will replace all the occurrences of that character, you could change your algorithm a little bit.
You can put the mapping into an object, which has the advantage of being a bit easier to maintain since the character and its replacement are adjacent in the object, rather than trying to align position in an array.
e.g.
var name = "przykłąd Ęś";
// Object of characters to replace and their replacement values
var charList = {'Ą':'A', 'ą':'a', 'Ć':'C', 'ć':'c', 'Ę':'E', 'ę':'e',
'Ł':'L', 'ł':'l', 'Ó':'O', 'ó':'o', 'Ś':'S', 'ś':'s',
'Ź':'Z', 'ź':'z', 'Ż':'Z', 'ż':'z'};
// For each character in the string, search for it in charList and if found,
// replace it with the value
alert(
name + '\n' + name.replace(/./g, function(c) {return c in charList? charList[c] : c})
);
There is likely something cleverer that can be done with char codes, but I can't think of it right now.
Edit 2017
Fixed last character mapping—thanks #MarekSkiba. :-)
Here my solution...
var cleanName = function(str) {
if ($.trim(str) == '') return str; // jQuery
str = $.trim(str).toLowerCase();
var special = ['&', 'O', 'Z', '-', 'o', 'z', 'Y', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', '.', ' ', '+', '\''],
normal = ['et', 'o', 'z', '-', 'o', 'z', 'y', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'd', 'n', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'n', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y', '_', '_', '-', '-'];
for (var i = 0; i < str.length; i++) {
for (var j = 0; j < special.length; j++) {
if (str[i] == special[j]) {
str = str.replace(new RegExp(str[i], 'gi'), normal[j]);
}
}
}
str = str.replace(/[^a-z0-9_\-]/gi, '_');
str = str.replace(/[\-]{2,}/gi, '_');
str = str.replace(/[\_]{2,}/gi, '_');
return str;
};
console.log(cleanName('l\'éléphant')); // "l-elephant"