Insert two consecutive elements in array at specific Index in Javascript - javascript

I have two arrays like this,
var firstArray = ['one','two','three','four','five','six','seven'];
var secondArray =['1','2','3','4','5','6','7','8'];
I have to insert second array elements into first array like this,
var combinedArray =['one','two','three','1','2','four','five','six','3','4','seven','5','6','7','8']
I know that I could splice and insert at specific index for one element. However I am confused how exactly to achieve this pattern.
Could any one help me out with this?

You could use a pattern for the chunks and slice the wanted length for a new array.
var firstArray = ['one', 'two', 'three', 'four', 'five', 'six', 'seven'],
secondArray = ['1', '2', '3', '4', '5', '6', '7', '8'],
data = [firstArray, secondArray],
pattern = [3, 2],
result = [],
i = 0,
l = data.reduce(function (r, a) { return Math.max(r, a.length); }, 0);
while (i < l) {
pattern.forEach(function (a, j) {
result = result.concat(data[j].slice(i * a, (i + 1) * a));
});
i++;
}
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

You can actually splice more than one item:
firstArray.splice(3, 0, "1", "2");

You can create two variables i and j and increment first one by 3 that you will use to slice first array and increment the second one by 2 and you will use that one to slice second array. If the i + 3 > a.length you will concat rest of elements in b array to result.
var a = ['one','two','three','four','five','six','seven'];
var b = ['1','2','3','4','5','6','7','8'];
var r = [], i = 0, j = 0
while(i < a.length) {
r.push(...a.slice(i, i + 3), ...b.slice(j, i + 3 < a.length ? j + 2 : b.length))
i += 3, j += 2
}
console.log(r)

a more granular approach:
var firstArray = ['one','two','three','four','five','six','seven'];
var secondArray =['1','2','3','4','5','6','7','8'];
var combinedArray = flatten(zip(
toGroupsOf(3, firstArray),
toGroupsOf(2, secondArray)
));
console.log(combinedArray);
//a the utilities for that
function isUint(value){
return value === (value >>> 0)
}
function toGroupsOf(length, arrayOrString){
if( !isUint(length) || !length )
throw new Error("invalid length " + JSON.stringify(length));
return Array.from(
{ length: Math.ceil(arrayOrString.length / length) },
(v,i) => arrayOrString.slice(i*length, (i+1)*length)
);
}
function zip(...arraysOrStrings){
var numColumns = arraysOrStrings.length,
lengths = arraysOrStrings.map(item => (item && +item.length) || 0),
x=0, y=0;
return Array.from(
{ length: lengths.reduce((a,b)=>a+b, 0) },
function(v,i){
for(var safety = numColumns+1; safety--;){
if(y < lengths[x])
return arrays[x++][y];
else if(++x >= numColumns)
x=0, ++y;
}
throw new Error("something went wrong, this line should have never been reached");
}
)
}
function flatten(array){
return [].concat.apply([], array);
}

Related

How to get all combinations of some arrays in Javascript

I see there's too many questions like this , but I did not find answer for my specific case
I need all the possible combinations for my input arrays
example
if we assume the input is [1,2];
the output should be : ["11","12","21","22"]
after research , I reached to this code
function perm(xs) {
let ret = [];
for (let i = 0; i < xs.length; i = i + 1) {
let rest = perm(xs.slice(0, i).concat(xs.slice(i + 1)));
if(!rest.length) {
ret.push([xs[i]])
} else {
for(let j = 0; j < rest.length; j = j + 1) {
ret.push([xs[i]].concat(rest[j]).join(""))
}
}
}
return ret;
}
it finds most of the combinations , but not all
the above code return only ["12","21"] for input [1,2]
however all the possible combinations should be > ["11","12","21","22"]
another example for input [1,2,3] , should have this output
["111","112","121","211","113","131","311","123","321","312","213","132" , "223" , "331" , "313" , "232" .. and so on
If the length of the generated strings should be equal to the size of the input array, then this recursive generator could be used:
function combiWithRepetitions(chars) {
function * recur(len, str) {
if (len) for (let chr of chars) yield * recur(len - 1, str + chr);
else yield str;
}
return [...recur(chars.length, "")];
}
let result = combiWithRepetitions([1, 2]);
console.log(result);
You are looking for permutation with repetitions that are of the same length as the input array. This code (adapted from: https://stackoverflow.com/a/30340475/9560885) gives you the output you want:
function perm(arr) {
var holdingArr = [];
var recursiveABC = function(singleSolution) {
if (singleSolution.length == arr.length) {
holdingArr.push(singleSolution);
return;
}
for (var i=0; i < arr.length; i++) {
recursiveABC(singleSolution.concat([arr[i]]).toString());
}
};
recursiveABC([]);
return holdingArr;
};
Example:
> perm([1,2])
[ '11', '12', '21', '22' ]
Another example:
> perm([1,2])
[ '111',
'112',
'113',
'121',
'122',
'123',
'131',
'132',
'133',
'211',
'212',
'213',
'221',
'222',
'223',
'231',
'232',
'233',
'311',
'312',
'313',
'321',
'322',
'323',
'331',
'332',
'333' ]
You could build a cartesian product of the wanted length with a generato function.
function getP(array, length) {
function* p(right = []) {
if (right.length === length) {
yield right.join('');
return;
}
for (const v of array) yield* p([...right, v])
}
return p();
}
console.log([...getP([1, 2, 3], 3)]);
Here's a one liner:
const f = (xs, l=xs.length) => l ? xs.flatMap(x => f(xs, l-1).flatMap(y => x + y)) : [''];
console.log(f([1, 2, 3]));

javascript leave max 3 occurrences of the same obj in an array

If I populate an array of pairs (inside a get json) like so:
var arrItems = [];
for (let y=0; y<50 ;y++){
var titl = data.item[y].name;
var img = data.item[y].image[0].url;
arrItems.push({t:titl,i:img});
}
How can I then filter it to leave only 3 pairs where value is the same?
Example:
arrItems = [
{t:one,i:square.jpg},
{t:two,i:square.jpg},
{t:three,i:square.jpg},
{t:four,i:square.jpg},
{t:five,i:triangle.jpg}
];
Becomes
arrItems = [
{t:one,i:square.jpg},
{t:two,i:square.jpg},
{t:three,i:square.jpg},
{t:five,i:triangle.jpg}
];
Both JavaScript or jQuery are OK.
You could take a hash table and count the occurences of the wanted property and filter with a max value.
var items = [{ t: 'one', i: 'square.jpg' }, { t: 'two', i: 'square.jpg' }, { t: 'three', i: 'square.jpg' }, { t: 'four', i: 'square.jpg' }, { t: 'five', i: 'triangle.jpg' }],
count = {},
result = items.filter(({ i }) => {
count[i] = (count[i] || 0) + 1;
return count[i] <= 3;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
There might be some more efficient ways to write it, but I would think the easiest to understand is the straightforward iteration with counting:
var counts = {};
for (var i = 0; i < arrItems.length; i++) {
var item = arrItems[i];
var count = counts[item.i] || 0;
if (count >= 3) {
arrItems.splice(i, 1);
i--;
} else {
counts[item.i] = ++count;
}
}
You can use reduce function
var arrItems = [
{t:'one',i:'square.jpg'},
{t:'two',i:'square.jpg'},
{t:'three',i:'square.jpg'},
{t:'four',i:'square.jpg'},
{t:'five',i:'triangle.jpg'}
];
var output = arrItems.reduce((acc, {t,i})=>{
acc['mem'][i] = (acc['mem'][i] || 0) + 1;
acc['mem'][i] <= 3 ? acc['output'].push({t, i}) : '';
return acc;
}, {'mem':{}, 'output':[]});
console.log(output);

Creating various arrays from a string using LOOPS

I have a string of values
"000111111122222223333333444455556666"
How could I use a loop to produce one array for index values from 0 to 3 (create an array of [000] and then another array of index values from 3 to 10, 10 to 17, 17 to 24, producing eg. [1111111, 2222222, 333333] and then another loop to produce an array of index values from 24 to 28, 28 to 32, 32 to 36, producing eg. [4444, 5555, 6666])?
So in total 3 different arrays have been created using three different for loops.
array1 = [000]
array2 = [1111111, 2222222, 333333]
array3 = [4444, 5555, 6666]
You may wish to try something line this (only a schematic solution!):
var l_Input = "000111111122222223333333444455556666" ;
var l_Array_1 = [] ;
var l_Array_2 = [] ;
var l_Array_3 = [] ;
var l_One_Char ;
for (var i = 0 ; i < l_Input.length ; i++) {
l_One_Char = l_Input.substring(i,i) ;
if (i < 3) {
l_Array_1.push(l_One_Char) ;
continue ;
}
if (i >= 3 && i < 10) {
l_Array_2.push(l_One_Char) ;
continue ;
}
:
:
}
I think this would work.
const str = '000111111122222223333333444455556666';
function makeArr(str, item) {
let firstIndex = str.indexOf(item);
let lastIndex = str.lastIndexOf(item) + 1;
return [ str.substring(firstIndex, lastIndex) ];
}
const first = makeArr(str, 0);
const second = [].concat(makeArr(str, 1))
.concat(makeArr(str, 2))
.concat(makeArr(str, 3));
const third = [].concat(makeArr(str, 4))
.concat(makeArr(str, 3))
.concat(makeArr(str, 3));
You could map the sub strings.
var str = '000111111122222223333333444455556666',
parts = [[3], [7, 7, 7], [4, 4, 4]],
result = parts.map((i => a => a.map(l => str.slice(i, i += l)))(0));
console.log(result);
function split(string, start, end) {
var result = [],
substring = string[start],
split;
for (var i = start + 1; i < end; i++) {
var char = string[i];
if (char === substring[0])
substring += char;
else {
result.push(substring);
substring = char;
}
}
result.push(substring);
return result;
}
split("00011122",0,8)
["000", "111", "22"]
To do this dynamically, you can use .split() and .map() methods to make an array from your string then group this array items by value.
This is how should be our code:
const str = "000111111122222223333333444455556666";
var groupArrayByValues = function(arr) {
return arr.reduce(function(a, x) {
(a[x] = a[x] || []).push(x);
return a;
}, []);
};
var arr = str.split("").map(v => +v);
var result = groupArrayByValues(arr);
This will give you an array of separate arrays with similar values each.
Demo:
const str = "000111111122222223333333444455556666";
var groupArrayByValues = function(arr) {
return arr.reduce(function(a, x) {
(a[x] = a[x] || []).push(x);
return a;
}, []);
};
var arr = str.split("").map(v => +v);
var result = groupArrayByValues(arr);
console.log(result);

Find all sentence permutations with synonymous words? [duplicate]

This question already has answers here:
Cartesian product of multiple arrays in JavaScript
(35 answers)
Closed 1 year ago.
I'm having trouble coming up with code to generate combinations from n number of arrays with m number of elements in them, in JavaScript. I've seen similar questions about this for other languages, but the answers incorporate syntactic or library magic that I'm unsure how to translate.
Consider this data:
[[0,1], [0,1,2,3], [0,1,2]]
3 arrays, with a different number of elements in them. What I want to do is get all combinations by combining an item from each array.
For example:
0,0,0 // item 0 from array 0, item 0 from array 1, item 0 from array 2
0,0,1
0,0,2
0,1,0
0,1,1
0,1,2
0,2,0
0,2,1
0,2,2
And so on.
If the number of arrays were fixed, it would be easy to make a hard coded implementation. But the number of arrays may vary:
[[0,1], [0,1]]
[[0,1,3,4], [0,1], [0], [0,1]]
Any help would be much appreciated.
Here is a quite simple and short one using a recursive helper function:
function cartesian(...args) {
var r = [], max = args.length-1;
function helper(arr, i) {
for (var j=0, l=args[i].length; j<l; j++) {
var a = arr.slice(0); // clone arr
a.push(args[i][j]);
if (i==max)
r.push(a);
else
helper(a, i+1);
}
}
helper([], 0);
return r;
}
Usage:
cartesian([0,1], [0,1,2,3], [0,1,2]);
To make the function take an array of arrays, just change the signature to function cartesian(args) instead of using rest parameter syntax.
I suggest a simple recursive generator function:
// JS
function* cartesianIterator(head, ...tail) {
const remainder = tail.length ? cartesianIterator(...tail) : [[]];
for (let r of remainder) for (let h of head) yield [h, ...r];
}
// get values:
const cartesian = items => [...cartesianIterator(items)];
console.log(cartesian(input));
// TS
function* cartesianIterator<T>(items: T[][]): Generator<T[]> {
const remainder = items.length > 1 ? cartesianIterator(items.slice(1)) : [[]];
for (let r of remainder) for (let h of items.at(0)!) yield [h, ...r];
}
// get values:
const cartesian = <T>(items: T[][]) => [...cartesianIterator(items)];
console.log(cartesian(input));
You could take an iterative approach by building sub arrays.
var parts = [[0, 1], [0, 1, 2, 3], [0, 1, 2]],
result = parts.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));
console.log(result.map(a => a.join(', ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
After doing a little research I discovered a previous related question:
Finding All Combinations of JavaScript array values
I've adapted some of the code from there so that it returns an array of arrays containing all of the permutations:
function(arraysToCombine) {
var divisors = [];
for (var i = arraysToCombine.length - 1; i >= 0; i--) {
divisors[i] = divisors[i + 1] ? divisors[i + 1] * arraysToCombine[i + 1].length : 1;
}
function getPermutation(n, arraysToCombine) {
var result = [],
curArray;
for (var i = 0; i < arraysToCombine.length; i++) {
curArray = arraysToCombine[i];
result.push(curArray[Math.floor(n / divisors[i]) % curArray.length]);
}
return result;
}
var numPerms = arraysToCombine[0].length;
for(var i = 1; i < arraysToCombine.length; i++) {
numPerms *= arraysToCombine[i].length;
}
var combinations = [];
for(var i = 0; i < numPerms; i++) {
combinations.push(getPermutation(i, arraysToCombine));
}
return combinations;
}
I've put a working copy at http://jsfiddle.net/7EakX/ that takes the array you gave earlier ([[0,1], [0,1,2,3], [0,1,2]]) and outputs the result to the browser console.
const charSet = [["A", "B"],["C", "D", "E"],["F", "G", "H", "I"]];
console.log(charSet.reduce((a,b)=>a.flatMap(x=>b.map(y=>x+y)),['']))
Just for fun, here's a more functional variant of the solution in my first answer:
function cartesian() {
var r = [], args = Array.from(arguments);
args.reduceRight(function(cont, factor, i) {
return function(arr) {
for (var j=0, l=factor.length; j<l; j++) {
var a = arr.slice(); // clone arr
a[i] = factor[j];
cont(a);
}
};
}, Array.prototype.push.bind(r))(new Array(args.length));
return r;
}
Alternative, for full speed we can dynamically compile our own loops:
function cartesian() {
return (cartesian.cache[arguments.length] || cartesian.compile(arguments.length)).apply(null, arguments);
}
cartesian.cache = [];
cartesian.compile = function compile(n) {
var args = [],
indent = "",
up = "",
down = "";
for (var i=0; i<n; i++) {
var arr = "$"+String.fromCharCode(97+i),
ind = String.fromCharCode(105+i);
args.push(arr);
up += indent+"for (var "+ind+"=0, l"+arr+"="+arr+".length; "+ind+"<l"+arr+"; "+ind+"++) {\n";
down = indent+"}\n"+down;
indent += " ";
up += indent+"arr["+i+"] = "+arr+"["+ind+"];\n";
}
var body = "var res=[],\n arr=[];\n"+up+indent+"res.push(arr.slice());\n"+down+"return res;";
return cartesian.cache[n] = new Function(args, body);
}
var f = function(arr){
if(typeof arr !== 'object'){
return false;
}
arr = arr.filter(function(elem){ return (elem !== null); }); // remove empty elements - make sure length is correct
var len = arr.length;
var nextPerm = function(){ // increase the counter(s)
var i = 0;
while(i < len)
{
arr[i].counter++;
if(arr[i].counter >= arr[i].length){
arr[i].counter = 0;
i++;
}else{
return false;
}
}
return true;
};
var getPerm = function(){ // get the current permutation
var perm_arr = [];
for(var i = 0; i < len; i++)
{
perm_arr.push(arr[i][arr[i].counter]);
}
return perm_arr;
};
var new_arr = [];
for(var i = 0; i < len; i++) // set up a counter property inside the arrays
{
arr[i].counter = 0;
}
while(true)
{
new_arr.push(getPerm()); // add current permutation to the new array
if(nextPerm() === true){ // get next permutation, if returns true, we got them all
break;
}
}
return new_arr;
};
Here's another way of doing it. I treat the indices of all of the arrays like a number whose digits are all different bases (like time and dates), using the length of the array as the radix.
So, using your first set of data, the first digit is base 2, the second is base 4, and the third is base 3. The counter starts 000, then goes 001, 002, then 010. The digits correspond to indices in the arrays, and since order is preserved, this is no problem.
I have a fiddle with it working here: http://jsfiddle.net/Rykus0/DS9Ea/1/
and here is the code:
// Arbitrary base x number class
var BaseX = function(initRadix){
this.radix = initRadix ? initRadix : 1;
this.value = 0;
this.increment = function(){
return( (this.value = (this.value + 1) % this.radix) === 0);
}
}
function combinations(input){
var output = [], // Array containing the resulting combinations
counters = [], // Array of counters corresponding to our input arrays
remainder = false, // Did adding one cause the previous digit to rollover?
temp; // Holds one combination to be pushed into the output array
// Initialize the counters
for( var i = input.length-1; i >= 0; i-- ){
counters.unshift(new BaseX(input[i].length));
}
// Get all possible combinations
// Loop through until the first counter rolls over
while( !remainder ){
temp = []; // Reset the temporary value collection array
remainder = true; // Always increment the last array counter
// Process each of the arrays
for( i = input.length-1; i >= 0; i-- ){
temp.unshift(input[i][counters[i].value]); // Add this array's value to the result
// If the counter to the right rolled over, increment this one.
if( remainder ){
remainder = counters[i].increment();
}
}
output.push(temp); // Collect the results.
}
return output;
}
// Input is an array of arrays
console.log(combinations([[0,1], [0,1,2,3], [0,1,2]]));
You can use a recursive function to get all combinations
const charSet = [["A", "B"],["C", "D", "E"],["F", "G", "H", "I"]];
let loopOver = (arr, str = '', final = []) => {
if (arr.length > 1) {
arr[0].forEach(v => loopOver(arr.slice(1), str + v, final))
} else {
arr[0].forEach(v => final.push(str + v))
}
return final
}
console.log(loopOver(charSet))
This code can still be shorten using ternary but i prefer the first version for readability 😊
const charSet = [["A", "B"],["C", "D", "E"],["F", "G", "H", "I"]];
let loopOver = (arr, str = '') => arr[0].map(v => arr.length > 1 ? loopOver(arr.slice(1), str + v) : str + v).flat()
console.log(loopOver(charSet))
Another implementation with ES6 recursive style
Array.prototype.cartesian = function(a,...as){
return a ? this.reduce((p,c) => (p.push(...a.cartesian(...as).map(e => as.length ? [c,...e] : [c,e])),p),[])
: this;
};
console.log(JSON.stringify([0,1].cartesian([0,1,2,3], [[0],[1],[2]])));

Distributing array elements randomly to new arrays

I have an array of numbers from 1 to 60
var originalArray = [1, 2, 3, 4 .... 58, 59, 60] // etc
I want to - depending on another number between 2 and 4 - split those numbers randomly into the number of arrays specified, and for the result to be unique each and every time.
For example:
distributeArray(2) should result in two arrays, each with 30 numbers randomly selected from the original array.
distributeArray(3) should result in three arrays, each with 20 numbers randomly selected from original array.
I assume this is a reasonably common case so any pointers would be appreciated. Thanks in advance.
You could do something like this, first shuffle and then split array into n parts.
var arr = [...Array(61).keys()].slice(1)
function splitRandom(data, n) {
var seen = [];
var counter = 0;
var shuffle = data.reduce(function(r, e) {
function random() {
var rand = parseInt(Math.random(0, arr.length) * arr.length);
if (seen.indexOf(rand) != -1) {
return random()
} else {
seen.push(rand)
return rand;
}
}
r[random()] = e;
return r;
}, [])
var split = shuffle.reduce(function(r, e) {
var c = counter++;
r[c] = r[c].concat(e)
counter = counter % n;
return r;
}, Array(n).fill([]))
return split;
}
console.log(JSON.stringify(splitRandom(arr, 3)))
console.log(JSON.stringify(splitRandom(arr, 10)))
console.log(JSON.stringify(splitRandom(arr, 50)))
You can create a function which creates an array of n .length, and an array of x .length. Use do..while loop Array.prototype.splice() to remove a random index from originalArray, .push() the element to one of x random arrays, until originalArray.length evaluates to false, return array of arrays containing values.
const randomArrays = (n, x) => {
let [originalArray, result, len] = [
Array.from({length: n}, (_, key) => key)
, Array.from({length: x}, () => [])
, Math.ceil(n / x)
];
do {
let [curr, index] = [
originalArray
.splice(Math.floor(Math.random() * originalArray.length), 1)
.pop()
, Math.floor(Math.random() * result.length)
];
if (result[index].length < len)
result[index].push(curr);
else
for (let i = 0; i < result.length; i++) {
if (result[i].length < len) {
result[i].push(curr);
break;
}
}
} while (originalArray.length);
return result
}
console.log(
randomArrays(60, 3)
, randomArrays(21, 7)
, randomArrays(5, 3)
, randomArrays(27, 5)
);

Categories

Resources