Is possible to combine two arrays? "array.join(array)"? [duplicate] - javascript

This question already has answers here:
zip two arrays Javascript [duplicate]
(3 answers)
Closed 8 years ago.
I have two arrays:
var array1= [[1],[2],[3]];
var array2= [[10],[20],[30]];
is there a way to (have) a third array?
var array3 = [[1],[10],[2],[20],[3],[30]];
Maybe:
var test= array1.join(array2 + "<br>");

I think you are looking for the "concat" function
Join two arrays:
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);
The values of the children array will be:
Cecilie,Lone,Emil,Tobias,Linus
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

You can either use CONCAT function like
array1 = array1.concat(array2)
OR, APPLY()
array1.push.apply(array1, array2)

Related

Javascript push and loop [duplicate]

This question already has answers here:
Create an array with same element repeated multiple times
(25 answers)
Closed 2 years ago.
I currently have this array: var arr = []
How do I push multiple "hello" strings into the array using a for loop?
I tried
var newArray = arr.push("hello")10;
try new Array forEach or simple for-loop should work.
var arr = [];
// method 1
new Array(5).fill(0).forEach(() => arr.push("hello"));
// alternate method
for (let i = 0; i < 5; i++) {
arr.push("world");
}
console.log(arr);
// Updating based on suggesion #mplungjan, quick way without loop.
var arr2 = Array(10).fill("hello");
console.log(arr2)

Find partial string in array [duplicate]

This question already has answers here:
How do you search an array for a substring match?
(15 answers)
Closed 3 years ago.
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"];
var n = fruits.includes("Mango");
Let's say you don't know whats inside the fruits array ?
How do you extract the string that contains mango.
The prefix string must be included in the result.
Can this be done without a for loop and parsing ?
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"];
var n = fruits.includes("Mango");
console.log(n)
You need to filter the array and check each string.
var fruits = ["CarPaint.InString.Banana", "CarPaint.InString.Orange", "CarPaint.InString.Apple", "CarPaint.InString.Mango"],
result = fruits.filter(string => string.includes("Mango"));
console.log(result);
How about the following?
fruits.filter(fruit => fruit.includes('Mango'))
// [ 'CarPaint.InString.Mango' ]

Add multiple options in array with javascript [duplicate]

This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 7 years ago.
I have this var
var allwords = {};
And i need push more options in this array like:
allwords.push = {"Ctrl-Space": "autocomplete"};
allwords.push = {"Ctrl-pause": "closewindow"};
And look like this:
allwords = {"Ctrl-Space": "autocomplete", "Ctrl-pause": "closewindow"};
How can't i do?
push is for Array objects. For traditional objects, assign the properties manually like
var allwords = {};
allwords["Ctrl-Space"] = "autocomplete";
allwords["Ctrl-pause"] = "closewindow";
console.log(allwords);

turn into 2d array index [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 7 years ago.
I have a array with 3*3 dimension(2d) like this:
a[3][3]=[[1,2,3],[4,5,6],[7,8,9]];
I want formulate this array to access in single array(1d). how I can do this?
like:
b[9]=[0,1,2,3,4,5,6,7,8,9];
you want to convert a 2d array to a flat array. How about:
var a = [[1,2,3],[4,5,6],[7,8,9]];
var merged = [];
merged = merged.concat.apply(merged, a);
see https://stackoverflow.com/a/10865042/1432801
var flatArray = [];
for(var i=0;i<a.length;i++){
for(var j=0;j<a[i].length;j++){
flatArray.push(a[i][j]);
}
}

javascript/jQuery sort array [duplicate]

This question already has answers here:
Sort Array Elements (string with numbers), natural sort
(8 answers)
Closed 8 years ago.
I have an array of values that look like this:
var myArr = ["S1_FORM", "S3_FORM", "S2_FORM", "S2_2_FORM"];
I need to sort them from lowest to highest.
The way I would like this array to be is like this:
["S1_FORM", "S2_FORM", "S2_2_FORM", "S3_FORM"]
Basically these numbers should be read like: 1, 2, 2.2, 3
How could I achieve this?
I have tried using .sort() but it returns:
["S1_FORM", "S2_2_FORM", "S2_FORM", "S3_FORM"]
notice "2_2" comes before "2_". It shouldn't.
var myArr = ["S1_FORM", "S3_FORM", "S2_FORM", "S2_2_FORM"];
var extractNumber = function(str) {
var m = str.match(/^S(\d+)_(?:(\d+)_)?/);
return parseFloat(m[1] + '.' + m[2])
};
myArr.sort(function(a, b) {
return extractNumber(a) - extractNumber(b);
});
console.log(myArr);
http://jsfiddle.net/LDphK/
So you're extracting a number using trivial regular expression and then sort it using Array.prototype.sort()
var myArr = ["S1_FORM", "S3_FORM", "S2_FORM", "S2_2_FORM"];
myArr.sort();
If you need another sorting logic, use jQuery. This is vanilla JavaScript solution

Categories

Resources