Get a all combinations of array elements [duplicate] - javascript

This question already has answers here:
Permutations in JavaScript?
(41 answers)
Is there any pre-built method for finding all permutations of a given string in JavaScript?
(8 answers)
Closed 17 days ago.
Example if a give A B C D i want get A AB AC ABCD B BA BC BD BACD BCAD BDAC BDCA...
i tried this code but this code is not given BCDA BCAD etc.
var letters = ["A", "B", "C", "D"]
var combi = [];
var temp= "";
var letLen = Math.pow(2, letters.length);
for (var i = 0; i < letLen ; i++){
temp= "";
for (var j=0;j<letters.length;j++) {
if ((i & Math.pow(2,j))){
temp += letters[j]
}
}
if (temp !== "") {
combi.push(temp);
}
}
console.log(combi.join("\n"));

Related

Mutate one letter of a string inside an array [duplicate]

This question already has answers here:
How do I replace a character at a particular index in JavaScript?
(30 answers)
Closed 1 year ago.
I would like to understand why I can't use this construction to capitalize de first letter of my strings of the array with JavaScript.
function capitalize(array){
for (let i = 0; i < array.length; i++){
array[i][0] = array[i][0].toUpperCase()
}
return lst;
}
I have already re-writed my code, to a way it works:
function capitalize(array){
for (let i = 0; i < array.length; i++){
array[i] = array[i][0].toUpperCase() + array[i].slice(1)
}
return array;
}
But I wanted to understand more deeply why
Strings are Immutable, so you can't modify a single letter of a string and have the original change, you have to assign an entirely new string.
let str = 'Hello';
str[0] = 'G' // doesn't actually do anything, since str is immutable
console.log(str)
str = 'G' + str.slice(1) // assigns a new string to str, so it works
console.log(str)
Strings are immutable. You need to reassign the entire string.
let arr = ["this", "is", "a", "test", "string"]
function capitalize(array){
for (let i = 0; i < array.length; i++){
array[i] = array[i].replace(array[i][0], array[i][0].toUpperCase() )
}
console.log( array);
}
capitalize(arr)

string conversion to numbers to avoid dupes [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
How to convert a string to an integer in JavaScript
(32 answers)
Closed 1 year ago.
function removeDups(arr) {
let x = {};
for (let i=0; i<arr.length; i++) {
if(!x[arr[i]]) {
x[arr[i]] = true;
}
}
let result = Object.keys(x);
for(let i=0; i<result.length; i++) {
if(typeof result[i] == Number) {
result[i] = parseInt(result[i]);
}
}
return result;
}
removeDups([1, 0, 1, 0]);
//➞ [1, 0]
Hey guys trying to return [1,0] like the problem states but I keep returning ['1','0']. I'm trying to convert these values to numbers and not having much luck. Any help?

How to save java script data from a loop to an array? [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Returning only certain properties from an array of objects in Javascript [duplicate]
(5 answers)
Javascript Array of objects get single value [duplicate]
(3 answers)
Construct an array of elements from an array of objects? [duplicate]
(2 answers)
Closed 4 years ago.
How to save javascript data from a loop to an array?
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
}
Insert the data in the array using push
var arr=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
arr.push(h);
}
var data = [];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
var h = jsonData.Data.Positions[i].Oid;
data.push(h)
}
//OR
var data = jsonData.Data.Positions.map(item => item.Oid);
Your variable jsonData.Data.Positions is probably already an array.
Use .push() method to add values to array.
var h=[];
for (i = 0; i < jsonData.Data.Positions.length; i++) {
h.push(jsonData.Data.Positions[i].Oid);
}
console.log(h);
You can do it within a loop:
var array = []
for (i = 0; i < jsonData.Data.Positions.length; i++) {
array.push(jsonData.Data.Positions[i].Oid);
}
Or in a more functional-way:
var array = jsonData.Data.Positions.map(p => p.Oid)
map instead of for loop
var h = jsonData.Data.Positions.map(function (x) { return x.0id });

How can I add an object to an array if object name is from another array? [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 6 years ago.
var streamerStatus = [];
var streamers = ['x', 'y', 'z'];
for (var i = 0; i < streamers.length; i++){
streamerStatus.push({streamers[i]: 'OFFLINE'});
}
for (var i = 0; i < streamers.length; i++){
var obj = {};
obj[streamers[i]] = 'OFFLINE';
streamerStatus.push(obj);
}

Output abnormal .. WHY? [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 6 years ago.
I have many days that I can not run this script!
var b = [9,8,7,6,5,4,3,2,1];
var a = (function(){
var W = [];
for(var k in b){
W[W.length] = {
index : k,
fx : function(){
console.log(k);
}
}
}
return W;
})();
console.log(a);
for(var j = 0; a[j]; j++)a[j].fx();
Because it does not produce as OUTPUT numerical sequence 987654321?
Each function fx that you create references the variable var k. They don't save the current value of k, only a reference to k. This means that when you run the functions, after the for loop has finished, the value of k is now 8, and they all print that.
Once way to avoid this, and give each function its own variable/value, is to change var k to let k (available since ECMAScript 6). See let at MDN.
var b = [9,8,7,6,5,4,3,2,1];
var a = (function(){
var W = [];
for (let k in b) {
W[W.length] = {
index : k,
fx : function(){
console.log(k);
}
}
}
return W;
})();
for(var j = 0; a[j]; j++)
a[j].fx();

Categories

Resources