create new array from combination of elements in array _ JavaScript - javascript

suppose we have this array :
let a = ["a", "b", "c"]
I need some combination like below:
[["a"], ["b"], ["c"], ["a", "b"], ["a", "c"], ["b", "c"], ["b", "a"], ["c", "a"], ["c","b"], ["a", "b", "c"], ...]
const a = ["a", "b", "c"];
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]))
}
}
}
return ret;
}
console.log(perm(a));
feel free to edit this question and if this is a similar question pls duplicate the question .strong text

after searching a while, I foundout there is a function :
function combination(A, comb = [], result = [comb]) {
for (var i = 0; i < A.length; i++)
result = result.concat(combination(A.slice(0, i).concat(A.slice(i + 1)), comb.concat(A[i])));
return result;
}
console.log(combination(["q", "a", "w"]))
and you can delete the first index with .slice(1, array.length)

Related

How to break lines in javascript using .join

I have 9 strings "a", "b", "c", "d", "e", "f", "g", "h", "i" in a JavaScript array.
const arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
I am printing the value of arr using JavaScript alert box.
but, if I use arr.join(" ") it is expected like:
a b c d e f g h i
but, I want to change the line for every 3rd element.
like:
a b c
d e f
g h i
How can I do this?
You can use a for loop with Array#slice.
const arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i"];
const parts = [];
for(let i = 0; i < arr.length; i += 3){
parts.push(arr.slice(i, i + 3).join(' '));
}
console.log(parts.join('\n'));
This is what I use to chunk arrays:
const chunkSize = 3;
array
.map((_, i) =>
i % itemsPerPage
? undefined
: items.slice(
i,
Math.floor(i / chunkSize) * chunkSize + chunkSize
)
)
.filter(($) => !!$);
So chunk it up and use:
chunkedArray.map((array) => array.join(" ")).join("\n");
The .map used changes all the arrays inside to a b c and then join all of them with a newline character :)
You can also do that with join by using template literal. e.g:
var arr = ["P613221", "W123456", "ew76879", "GDG767"]
var stringWithQuotes = `'${arr.join(`', '`)}'`
console.log({
stringWithQuotes,
query: `SELECT * FROM users WHERE id IN (${stringWithQuotes})`
})
output:
"P613221', 'W123456', 'ew76879', 'GDG767"

How to get the max number and key

I have has like this
var hash = {};
hash['a'] = ["a", "b", "c", "d", "e"];
hash['b'] = ["a", "b", "c"];
hash['k'] = ["q", "b"];
Math.max(Object.keys(hash)
.map(function(key) {
return hash[key].length;
})
)
It returns 5 but, I want to get the hash key 'a' at the same time.
Is it possible??
Combine Object.entries() and Array.prototype.reduce() to get the key and the elements behind that key (and their length)
var hash = {
a: ["a", "b", "c", "d", "e"]
,b: ["a", "b", "c"]
,k: ["q", "b"]
};
var result = Object.entries(hash)
.reduce((r, c) => r[1].length > c[1].length ? r : c);
console.log(result);
Does this fulfil your requirement?
I have used sort instead of Math.max here to find the maxima
var hash = {};
hash['a'] = ["a", "b", "c", "d", "e"];
hash['b'] = ["a", "b", "c"];
hash['k'] = ["q", "b"];
var result = Object.keys(hash)
.map(function(key) {
return {[key]: hash[key].length};
}).sort(function(a, b){
return a.key > b.key
})[0]
console.log(result);
i have used this approach for your problem:
var hash = {};
hash['a'] = ["a", "b", "c", "d", "e"];
hash['b'] = ["a", "b", "c"];
hash['k'] = ["q", "b"];
var largestInfo = {
key:'',
length:0
}
Object.keys(hash).map(function(key) {
if(hash[key].length > largestInfo.length){
largestInfo.key = key;
largestInfo.length = hash[key].length;
}
return hash[key].length;
});
console.log(largestInfo);

Javascript: How do I check if an array contains exactly another array?

Suppose I have arrays parent and child. I want to check if a child exists inside a parent array. The ordering matters.
Example:
parent = ["x", "a", "b", "c", "d", "e", "f", "g"]
child = ["a", "b", "c"]
//returns true
Example:
When parent has a different order:
parent = ["x", "g", "b", "c", "d", "e", "f", "a"]
child = ["a", "b", "c"]
//It should return false
How would I achieve this in Javascript?
Edit: I have tried this How to check if an array contains another array? but it did not work for my case
You can run a loop for child and change the index accordingly. You can also use a match variable to detect change in the sequence.
RETURN TRUE
var parent = ["x", "a", "b", "c", "d", "e", "f", "g"]
var child = ["a", "b", "c"];
var initIndex = parent.indexOf(child[0]);
var match = true;
for(var i=1; i<child.length; i++){
var varIndex = parent.indexOf(child[i]);
if( varIndex === initIndex+1){
initIndex = varIndex;
continue;
}
match = false;
}
console.log(match);
RETURN FALSE
var parent = ["x", "g", "b", "c", "d", "e", "f", "a"]
var child = ["a", "b", "c"];
var initIndex = parent.indexOf(child[0]);
var match = true;
for(var i=1; i<child.length; i++){
var varIndex = parent.indexOf(child[i]);
if( varIndex === initIndex+1){
initIndex = varIndex;
continue;
}
match = false;
}
//return false
console.log(match);
USING STRING OPERATION
You can also convert the array to string to avoid those loop:
var parent = ["x", "g", "b", "c", "d", "e", "f", "a"]
var child = ["a", "b", "c"]
var parentStr = parent.toString();
var match = parentStr.indexOf(child.toString()) !== -1;
//return false
console.log(match);
parent = ["x", "a", "b", "c", "d", "e", "f", "g"]
child = ["a", "b", "c"]
parentStr = parent.toString();
match = parentStr.indexOf(child.toString()) !== -1;
//return true
console.log(match);
Convert the array to string by using JSON.stringify() and remove the square brackets from child string.
Now check the indexOf child in parent to check if it contains the child.
let parent = ["x", "a", "b", "c", "d", "e", "f", "g"];
let child = ["a", "b", "c"];
var parStr = JSON.stringify(parent);
var chldStr = JSON.stringify(child).replace('[', '').replace(']', '')
console.log(parStr.indexOf(chldStr) !== -1);
I wrote a function for this a while back that takes some paramenters:
Array.prototype.containsArray = function (child, orderSensitivity, caseSensitivity, typeSensitivity) {
var self = this;
if (orderSensitivity) return orderSensitiveComparer();
else return orderInsensitiveComparer();
function orderSensitiveComparer() {
var resultArry = [],
placeholder = 0;
if (child.length > self.length) return false;
for (var i = 0; i < child.length; i++) {
for (var k = placeholder; k < self.length; k++) {
if (equalityComparer(self[k], child[i])) {
resultArry.push(true);
if (resultArry.length === child.length) return true;
placeholder = k + 1;
break;
}
else resultArry = [];
}
}
return false;
}
function orderInsensitiveComparer() {
for (var i = 0; i < child.length; i++) {
var childHasParentElement = false;
for (var k = 0; k < self.length; k++) {
if (equalityComparer(child[i], self[k])) {
childHasParentElement = true;
break;
}
}
if (!childHasParentElement) return false;
}
return true;
}
function equalityComparer(a, b) {
if (caseSensitivity && typeSensitivity) return caseSensitiveEq(a, b) && typeSensitiveEq(a, b);
else if (!caseSensitivity && typeSensitivity) return caseInsensitiveEq(a, b) && typeSensitiveEq(a, b);
else if (caseSensitivity && !typeSensitivity) return caseSensitiveEq(a, b) && typeInsensitiveEq(a, b);
else if (!caseSensitivity && !typeSensitivity) return caseInsensitiveEq(a, b) && typeInsensitiveEq(a, b);
else throw "Unknown set of parameters";
function caseSensitiveEq(a, b) {
return a == b;
}
function caseInsensitiveEq(a, b) {
return (a + "").toLowerCase() == (b + "").toLowerCase();
}
function typeSensitiveEq(a, b) {
return typeof(a) === typeof(b);
}
function typeInsensitiveEq(a, b) {
return true;
}
}
}
var parent = [1, 2, 3, "a", "b", "c"];
var child = [1, 2, 3];
var child2 = ["1", "2", "3"];
var child3 = ["A", "b", "C"];
var child4 = ["a", "b", "c"];
var child5 = ["c", "b", "a"];
// Tests:
console.log(parent.containsArray(parent));
console.log(parent.containsArray(child));
console.log(parent.containsArray(child2));
// parent to child 2, order sensitive, not case, not type. => true.
console.log(parent.containsArray(child2, true, false, false));
// parent to child 2, order, not case, type. => false. b/c of type.
console.log(parent.containsArray(child2, true, false, true));
// parent to child 3, order, not case, type. => true.
console.log(parent.containsArray(child3, true, false, true));
// parent to child 4, order, case and type => true.
console.log(parent.containsArray(child4, true, true, true));
// parent to child 4, not order, case and type. => true.
console.log(parent.containsArray(child4, false, true, true));
// parent to child 5, not order case or type => true.
console.log(parent.containsArray(child5));
I have a simple method for small sized arrays of this problem.
First join the array to a string, see Array/join
Search substring, see String/indexOf
parent = ["x", "a", "b", "c", "d", "e", "f", "g"];
child = ["a", "b", "c"];
function matchSubArray(parent, child) {
parentStr = parent.join('');
childStr = child.join('');
return parentStr.indexOf(childStr) != -1;
}
matchSubArray(parent, child);
var parent = ["x", "g", "b", "c", "d", "e", "f", "a"]
var child = ["a", "b", "c"]
if(parent.join("").search(child.join("")) === -1) {
console.log("Not found");
} else {
console.log("found")
}

How to remove all the odd indexes (eg: a[1],a[3]..) value from the array

I have an array like var aa = ["a","b","c","d","e","f","g","h","i","j","k","l"]; I wanted to remove element which is place on even index. so ouput will be line aa = ["a","c","e","g","i","k"];
I tried in this way
for (var i = 0; aa.length; i = i++) {
if(i%2 == 0){
aa.splice(i,0);
}
};
But it is not working.
Use Array#filter method
var aa = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"];
var res = aa.filter(function(v, i) {
// check the index is odd
return i % 2 == 0;
});
console.log(res);
If you want to update existing array then do it like.
var aa = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"],
// variable for storing delete count
dCount = 0,
// store array length
len = aa.length;
for (var i = 0; i < len; i++) {
// check index is odd
if (i % 2 == 1) {
// remove element based on actual array position
// with use of delete count
aa.splice(i - dCount, 1);
// increment delete count
// you combine the 2 lines as `aa.splice(i - dCount++, 1);`
dCount++;
}
}
console.log(aa);
Another way to iterate for loop in reverse order( from last element to first ).
var aa = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"];
// iterate from last element to first
for (var i = aa.length - 1; i >= 0; i--) {
// remove element if index is odd
if (i % 2 == 1)
aa.splice(i, 1);
}
console.log(aa);
you can remove all the alternate indexes by doing this
var aa = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"];
for (var i = 0; i < aa.length; i++) {
aa.splice(i + 1, 1);
}
console.log(aa);
or if you want to store in a different array you can do like this.
var aa = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"];
var x = [];
for (var i = 0; i < aa.length; i = i + 2) {
x.push(aa[i]);
}
console.log(x);
You can use .filter()
aa = aa.filter((value, index) => !(index%2));
You can use temporary variable like below.
var a = [1,2,3,4,5,6,7,8,9,334,234,234,234,6545,7,567,8]
var temp = [];
for(var i = 0; i<a.length; i++)
if(i % 2 == 1)
temp.push(a[i]);
a = temp;
in Ecmascript 6,
var aa = ["a","b","c","d","e","f","g","h","i","j","k","l"];
var bb = aa.filter((item,index,arr)=>(arr.splice(index,1)));
console.log(bb);
const aa = ["a","b","c","d","e","f","g","h","i","j","k","l"];
let bb = aa.filter((items, idx) => idx % 2 !== 0)
I read here that splice has O(N) time complexity. Don't use it in a loop!
A simple alternative for removing odd indexes in place:
for (let idx = 0; idx < aa.length; idx += 2)
aa[idx >> 1] = aa[idx];
aa.length = (aa.length + 1) >> 1;
I use x >> 1 as a shortcut to Math.floor(x/2).

How to Generate 3x3 HTML Table From an JavaScript Array

I have an array of values in Javascript as follows:
var data = {"A", "B", "C", "D", "E", "F", "G", "H", "I" };
How can I generate a 3x3 HTML table like the following
A B C
D E F
G H I
var data = ["A", "B", "C", "D", "E", "F", "G", "H", "I" ]; // corrected Array syntax
var table = document.createElement("table");
var i = 0;
for (var r = 0; r < 3; r++) {
var row = table.insertRow(-1);
for (var c = 0; c < 3; c++) {
var cell = row.insertCell(-1);
cell.appendChild(document.createTextNode(data[i++]));
}
}
document.body.appendChild(table);
var data = ["A", "B", "C", "D", "E", "F", "G", "H", "I" ];
var html = '<table><tr>';
for(var i = 0 ; i<data.length; i++){
if(i%3 == 0 && i != 0){
html+= '</tr><tr>'
}
html+='<td>'+data[i]+'</td>';
}
html+='</tr></table>';
document.write(html);
http://jsfiddle.net/DuSP5/ just tried this one.

Categories