I am trying to get all anagrams from a array of words:
arr = ['cab','bac','tru']
The expected output is supposed to be:
{abc: ['cab','bac']}
I tried to implement the below code:
var words = ['cab', 'bac', 'mihir']
let result = {}
words.forEach(word => {
var a = word.split("").sort().join("");
result[word] = a
})
console.log(result)
How do I iterate over the values so that I can access the keys if they have the same values?
You can use the sorted words as keys in an object, and collect each word that matches the key in an array:
var words = ['cab', 'bac', 'mihir']
let result = {}
for (const word of words) {
const sorted = word.split("").sort().join("");
if (sorted in result) {
// If there is already an entry in the result, append this word
result[sorted].push(word);
} else {
// Otherwise, create one
result[sorted] = [word];
}
}
console.log(result);
Related
I am new to JavaScript and want to process the following array -
var a = [
"John-100",
"Mark-120",
"John-50",
"Mark-130"
]
into the following format -
a = {
"John": [100, 50],
"Mark": [120, 130]
}
But have been unable to do so. Any help will be very much appreciated.
Edit - Any other format ideas where the marks of a particular student can be grouped together are also welcome.
Here is one way to achieve what you described:
var a=[
"John-100",
"Mark-120",
"John-50",
"Mark-130"
]
function convertToSpecialObject(input) {
//setup the output as an empty object
const output = {};
// iterate through input array one element at a time
input.forEach(e => {
// split the current element by dividing it into part[0] before the dash
// and part[1] after the dash sign
const parts = e.split(/-/);
// now check the output object if it already contains a key for the part before the dash
if(!output[parts[0]]) {
// in this case, we don't have a key for it previously
// so lets set it up as a key with an empty array
output[parts[0]] = [];
}
// we must have already created a key or there is a key in existence
// so let's just push the part after the dash to the current key
output[parts[0]].push(Number(parts[1]));
});
// work done
return output;
}
const b = convertToSpecialObject(a);
console.log(b);
you can achieve this by using reduce and split method
var a=[
"John-100",
"Mark-120",
"John-50",
"Mark-130"
]
const b = a.reduce((acc, val) => {
const _split = val.split('-');
const name = _split[0]
if(acc && acc[name]) {
acc[name].push(+_split[1])
} else {
acc[name] = [+_split[1]]
}
return acc;
}, {});
console.log(b)
You can achieve it in a very simple way by just using a Array.forEach() method along with the String.split().
Live Demo :
var a = [
"John-100",
"Mark-120",
"John-50",
"Mark-130"
];
const obj = {};
a.forEach(element => {
if (!obj[element.split('-')[0]]) {
obj[element.split('-')[0]] = [];
}
obj[element.split('-')[0]].push(element.split('-')[1])
});
console.log(obj);
With Simple Approach
const input = [
"John-100",
"Mark-120",
"John-50",
"Mark-130"
];
const getCustomObject = (arr) => {
const obj = {};
for (let i = 0; i < arr.length; i++) {
const split = arr[i].split('-'); //spliting with '-'
if (obj[split[0]]) {
//push to existing array
obj[split[0]].push(split[1]);
} else {
obj[split[0]] = []; //initilize array if no member
obj[split[0]].push(split[1]);
}
};
return obj;
}
console.log(getCustomObject(input));
Now numbers are not numerical values, It can be achieved with parseInt or parseFloat
As I suggested, string split, and array reduce - add in an array map and it's a single line of code
let a=["John-100","Mark-120","John-50","Mark-130"];
a=a.map(v=>v.split('-')).reduce((r,[n,m])=>({...r,[n]:[...r[n]||[],+m]}),{});
console.log(JSON.stringify(a));
The only answer with the correct result ... an array of NUMBERS
I'm trying to get an output like this:
['h', 'ee', 'lll', 'llll', 'ooooo']
currently my out put is:
[ 'h', 'ee', 'lll', 'lll', 'ooooo' ]
The issue is the second occurrence of the "l" isn't being repeated one more time because I'm counting the index of the letter then adding 1 and it is only counting the first occurrence of the "l".
This is the code I have so far, any help would be great.
function mumble(string) {
string.toLowerCase();
let arrayPush = [];
let array = string.split("");
let count = 0;
let char = [];
array.map((letter) => {
count = array.indexOf(letter);
arrayPush.push(letter.repeat(count + 1));
});
return arrayPush;
}
console.log(mumble("hello"));
Don't use indexOf, use the second parameter in the .map callback to determine the index (and from that, the number of times to repeat the string).
function mumble(string) {
string.toLowerCase();
let arrayPush = [];
let array = string.split("");
let count = 0;
let char = [];
array.map((letter, i) => {
arrayPush.push(letter.repeat(i + 1));
});
return arrayPush;
}
console.log(mumble("hello"));
Instead of applying .map to an array split from the string, you can simply loop through the string, accessing each character using the .charAt() string method, and pushing it's .repeat() product to the result array.
Working snippet:
console.log(mumble('hELlo'));
function mumble(string) {
const result = [];
for(let i=0; i<string.length; i++) {
result.push(string.toLowerCase().charAt(i).repeat(i+1));
}
return result;
} // end function mumble;
I'm working on a JS applet that requires replacement of array entries in a given string with entires from another array. This is my code as it now stands:
const string = "Lion, Unicorn, Unicorn";
const array1 = ["Lion", "Unicorn"];
const array2 = ["Fox", "Hound"];
const string2 = string.replaceAll(array1[0],array2[0]) //returns "Fox, Unicorn, Unicorn"
My desired output is :
Fox, Hound, Hound.
Specifically, I'd like to turn the contents of string2 into a function that repeats for every item in an array, but have no idea where to start.
Thanks!
Do you mean something like this?
I hope I understood the question well.
You can write a recursive function:
let string = "Lion, Unicorn, Unicorn";
let array1 = ["Lion", "Unicorn"];
let array2 = ["Fox", "Hound"];
function myCustomReplace(str, a1, a2) {
let wordToReplace=a1.shift(); // a1[0] - if array change matters
let replacementWord=a2.shift(); // a2[0] - if array change matters
if (!wordToReplace || !replacementWord) return str;
str=str.replaceAll(wordToReplace, replacementWord );
return myCustomReplace(str,a1,a2); // rturn myCustomReplace(str,a1.slice(1),a2.slice(1)) - if array change matters
}
console.log(
myCustomReplace(string,array1,array2)
)
It's sometimes worthwhile to first transform the inputs into a shape that is easier to work on. For this problem, the input sentence is better thought of as an array of words, and the two arrays used for replacement are better represented as a single object mapping input words to output words...
let string = "Lion, Unicorn, Unicorn";
let array1 = ["Lion", "Unicorn"];
let array2 = ["Fox", "Hound"];
// transform the inputs
let input = string.split(", ");
let translator = array1.reduce((acc, key, i) => {
acc[key] = array2[i];
return acc;
}, {});
// now input is ['Lion', 'Unicorn', ...]
// and transator is { 'Lion' : 'Fox', ... }
// now the problem is just a two-liner, mapping the input over the translator
let output = input.map(e => translator[e] || e)
console.log(output.join(", "))
If we use split(', ') to convert the string to an array of single words, we can use map() to replace them by searching for a pair with indexOf():
Please see comments in the code. A one-liner can be found at the end.
const string = "Lion, Unicorn, Unicorn";
const array1 = ["Lion", "Unicorn"];
const array2 = ["Fox", "Hound"];
// Split on ', '
let splitted = string.split(', ');
// Map
let result = splitted.map(w => {
// Get position in array1
const i = array1.indexOf(w);
// If we've found something
if (i !== -1) {
// Return replacement
return array2[i];
} else {
// Return original
return w;
}
});
// Create string
result = result.join(', ');
// Show
console.log(result);
// Or, as a one-liner
let result2 = string.split(', ').map(w => (array1.indexOf(w) !== -1) ? array2[array1.indexOf(w)] : w).join(', ');
console.log(result2);
I am trying to get an array of strings from another array of strings with following conditions :-
1: Should contain concatenation of first letter of the word at the first index of array with all other elements EX - ["AH", "BEE", "JAMES"] --> ["ABEEJAMES"]
2: Should contain concatenation of first letter of the word at the first and second index of array with all other elements EX - ["AH", "BEE", "JAMES"] --> ["ABJAMES"]
This needs to be done till the last index of the array
Input array of strings
const updateFullNameParts = ["AH", "BEE", "JAMES"];
Current implementation
const noSpaceName = updateFullNameParts.join("");
const createCombination = fullNameParts.map((name) => {
const fullNamePartsCopy = fullNameParts;
const innerResult = fullNamePartsCopy.map((nameCopy) => {
let innerCopy = "";
for (let i = 0; i < fullNamePartsCopy.length; i++) {
if (name === fullNamePartsCopy[i]) {
innerCopy = innerCopy + fullNamePartsCopy[i];
} else if (nameCopy === fullNamePartsCopy[i])
innerCopy = innerCopy + fullNamePartsCopy[i];
else innerCopy = innerCopy + fullNamePartsCopy[i].slice(0, 1);
}
return innerCopy;
});
Expected array of strings
["ABEEJAMES", "ABJAMES", "ABJ", "AHBJAMES", "ABEEJ", "AHBEEJAMES", "AHBEE"]
Array should not contain
["BJAMES", "AB", "BAJAMES", "A"]
Basically the array should only contain combinations in the order of index.
Is there a low time complexity solution for this?
First make list from each word. (first letter and whole word).
Then create list of possible words using couple of forEach loops.
const updateFullNameParts = ["AH", "BEE", "JAMES"];
const list = (arr) => {
let output = [""];
arr.forEach((word) => {
const items = [word[0], word];
const temp = [];
output.forEach((xx) => items.forEach((yy) => temp.push(`${xx}${yy}`)));
output = [...temp];
});
// Add special cases.
// First word and second word
output.push(arr.slice(0, 2).join(''));
return output;
};
console.log(list(updateFullNameParts));
Another variant and simplified with reduce
const arr = ["AH", "BEE", "JAMES"];
const list = arr.reduce(
(acc, word) => [word[0], word].flatMap((yy) => acc.map((xx) => `${xx}${yy}`)),
[""]
);
console.log(list);
I have an array which looks like
var arr = ["a|c", "a|e", "x|z"];
for(var x in arr){
var appsplit = x.split("|");
}
If the first value(ex: a) in the elements matches then it should combine the values
Ex: output
ace
xz
Please advice how this approach can be done.
You are testing everyone's reading comprehension with that riddle.
var pairs = {};
var arr = ["a|c", "a|e", "x|z"];
for(var x in arr)
{
var appsplit = arr[x].split("|");
if(pairs[appsplit[0]] !== "undefined")
{
pairs[appsplit[0]] = pairs[appsplit[0]] + appsplit[1];
}
else
{
pairs[appsplit[0]] = appsplit[1];
}
}
var matches = [];
for(var x in pairs)
{
matches.push(x + pairs[x]);
}
console.log(matches);
We need to map out the arr elements in this object called pairs. The first value in your split would be the key and the second value is appended (or assigned if it's the first match to the key)
You made an error of splitting x, but you are only splitting the index of the element, not the actual value of the element. arr[x] is the actual value, where x specifies the index in the array.
After we've gone through your arr, we can now merge the key with the values. Your output is contained in matches where the key in each pair is prepended to the value of the key's pair.
Some simple code that would to the trick here.
var arr = ["a|c", "a|e", "x|z", "c|b", "z|e", "c|a"];
var resultObj = {};
arr.forEach(function(element, index){
var array = element.split('|');
if(array.length!==2){
console.log("skipping, invalid input data", element);
} else {
var firstLetter = array[0];
var secondLetter = array[1];
if(resultObj[firstLetter]){
resultObj[firstLetter].push(secondLetter);
} else {
resultObj[firstLetter]=[secondLetter];
}
}
});
Object.keys(resultObj).forEach(function(key){
console.log(key + "," + resultObj[key]);
});
You can use .reduce(), Set to not accumulate duplicate values, .some() to check if previous array contains value in current array, .map(), Array.from() and .join() to convert array to string
var arr = ["a|c", "a|e", "x|z"];
var res = arr.reduce(function(a, b) {
var curr = b.split("|");
var set = new Set;
for (let prop of curr) set.add(prop);
if (!a.length) {
a.push(set)
} else {
for (prop of a) {
if (curr.some(function(el) {
return prop.has(el)
})) {
for (el of curr) {
prop.add(el)
}
} else {
for (let prop of curr) set.add(prop);
a.push(set)
}
}
}
return a
}, []).map(function(m) {
return Array.from([...m], function(el) {
return el
}).join("")
});
console.log(res);
I feel like this can be done more elegantly, but I didn't have time to streamline it. :) The below code will do what you want, though:
var aStartArray = **ARRAY_VALUE_HERE**;
var aSplitResultStrings = [];
// step through each element in the array
for (var i = 0, iSALength = aStartArray.length; i < iSALength; i++) {
// split the values for the current array element
var aSplitVal = aStartArray[i].split("|");
var bStringDoesNotExist = true;
// loop through the "result strings" array
for (var j = 0, iSRSLength = aSplitResultStrings.length; j < iSRSLength; j++) {
// if the first letter from the array element = the first letter of the current "result string" . . .
if (aSplitResultStrings[j].charAt(0) === aSplitVal[0]) {
// append the second letter of the array value to the current result string
aSplitResultStrings[j] = aSplitResultStrings[j] + aSplitVal[1];
// indicate that a match has been found and exit the "result string" loop
bStringDoesNotExist = false;
break;
}
}
// if there are no result strings that start with the first letter of the array value . . .
if (bStringDoesNotExist) {
// concatenate the two values in the current array value and add them as a new "result string"
aSplitResultStrings.push(aSplitVal[0] + aSplitVal[1]);
}
}
Using these arrays, the results are:
aStartArray = ["a|c", "a|e", "x|z"] //results in:
aSplitResultStrings = ["ace", "xz"]
aStartArray = ["a|b", "a|c", "a|d", "a|e", "x|y", "x|z"] //results in:
aSplitResultStrings = ["abcde", "xyz"]
aStartArray = ["a|b", "d|e", "d|f", "x|y", "g|h", "g|i", "m|n", "g|j", "a|c", "x|z"] //results in:
aSplitResultStrings = ["abc", "def", "xyz", "ghij", "mn"]
As I said, this could be more elegant (for example, you could probably use Map to make iterating through the "result strings" easier), but this makes the steps pretty clear and should get you going down the right path towards a final solution.