Concat javascript arrays as array of arrays [duplicate] - javascript

This question already has answers here:
How can I create a two dimensional array in JavaScript?
(56 answers)
Closed 5 years ago.
I have these arrays:
arr1 = ['29.5', 32035];
arr2 = ['30.5', 32288];
arr3 = ['31.5', 31982];
arr4 = ['1.6', 31768];
As a result I want to have something like this:
result = [['29.5', 32035], ['30.5', 32288], ['31.5', 31982], ['1.6', 31768]];
I means the result is array created by another arrays. The question is, how I can concat the arrays. result.push.apply(result, arr1); etc. give me array made by final values.
Thank you for any advice.

Actually you can just do
var result = [arr1, arr2, arr3, arr4];
Or
var result = [];
result[0] = arr1;
result[1] = arr2;
result[2] = arr3;
result[3] = arr4;
Or
var result = [];
result.push(arr1);
result.push(arr2);
result.push(arr3);
result.push(arr4);

You can do this using push .
var result = [];
result.push(arr1);
result.push(arr2);
result.push(arr3);
result.push(arr4);
console.log(result);

Related

array.push() and splite in Array of arrays [duplicate]

This question already has answers here:
Split array into chunks
(73 answers)
Closed 6 days ago.
This post was edited and submitted for review 6 days ago.
I'm using arrayofArrays[0].push() inside a for loop in order to add an Object to the Array0 inside arrayofArrays.
Now I'd like that when Array0 has reached 2 element, the next element will be pushed to Array1, in order to achieve this situation:
var arrayofArrays = [[Obj0,Obj1],[Obj2,Obj3],[Obj4,Obj5], ...];
Sample code:
var arrayofArrays = [[]];
for(data in Data){
var Obj = {"field1":data[0], "field2":data[1], "field3":data[2] }
arrayofArrays[0].push(Obj); // need to pass to arrayofArrays[1] when arrayofArrays[0] has 2 Obj...
}
(I don't need to split an existing array, I'm adding Object to an array, and want them to split in sub-arrays while adding them)
Here is a functional programming approach to your question to unflatten an array:
const arr = Array.from(Array(6).keys()); // [0, 1, 2, 3...]
let t;
let arrOfArr = arr.map((v, idx) => {
if(idx % 2) {
return [t, v];
} else {
t = v;
return null;
}
}).filter(Boolean);
console.log({
arr,
arrOfArr,
});
Note: Array.from(Array(6).keys()) is just for demo. Replace this with any array of objects you like

Create array with many arrays inside which are created from one big array on special condition [duplicate]

This question already has answers here:
Transposing a 2D-array in JavaScript
(25 answers)
Closed 2 years ago.
// n number of those
let array1 = [1,3,3,6]
let array2 = [4,7,3,8]
let array3 = [1,4,6,4]
// wanted
let final = [
[1,4,1], <-- first array in the final
[3,7,4],
[3,3,6],
[6,8,4]
]
First from each array (array1, array2, array3...) create first array in final one.
Second from each array create second one.. etc.
Any ideas?
You can do something like this:
const final = [];
for (let i = 0; i < array1.length; ++i) {
final[i] = [array1[i], array2[i], array3[i]];
}
console.dir(final);
you can try this
let array1 = [1,3,3,6]
let array2 = [4,7,3,8]
let array3 = [1,4,6,4]
finarray=[]
array1.forEach((x,i)=>{ finarray.push([x,array2[i],array3[i]])})
console.log(finarray)

Finding match element with array reduce [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
I have two arrays
a. [1,2,3,4,5]
b. [2,3,4,5,6]
I try to find 2,3,4,5 with array.reduce because I think it is more efficient.
Can I do so?
This will get you the same result without using reduce:
var a=[1,2,3,4,5];
var b= [2,3,4,5,6];
result = a.filter(p=>b.includes(p));
console.log(result);
Or with reduce:
var a=[1,2,3,4,5];
var b= [2,3,4,5,6];
var result = b.reduce((acc,elem)=>{
if(a.includes(elem)) acc.push(elem);
return acc;
},[]);
console.log(result);
With filter and includes
{
const a = [1,2,3,4,5];
const b = [2,3,4,5,6];
let overlap = a.filter(e => b.includes(e))
console.log(overlap)
}

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)

AngularJS objects in an array to new one [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 5 years ago.
DashboardService.GetDateList($scope.datestart, $scope.dateend).then(function (response) {
$scope.listdate = response.data;
});
i get an array list from this function above
[{"day":1,"sql_date":"2017-04-01T00:00:00"},
{"day":2,"sql_date":"2017-04-02T00:00:00"},
{"day":3,"sql_date":"2017-04-03T00:00:00"},
{"day":4,"sql_date":"2017-04-04T00:00:00"},
{"day":5,"sql_date":"2017-04-05T00:00:00"}
how can i push all day value from this array into a new one.
You can use Array#map to get the value of every day key.
var arr = [{"day":1,"sql_date":"2017-04-01T00:00:00"},{"day":2,"sql_date":"2017-04-02T00:00:00"},{"day":3,"sql_date":"2017-04-03T00:00:00"},{"day":4,"sql_date":"2017-04-04T00:00:00"},{"day":5,"sql_date":"2017-04-05T00:00:00"}],
newArr = arr.map(v => v.day);
console.log(newArr);
You can achieve this in different ways :
Using JavaScript for...in loop.
DEMO
var responseObj = [{"day":1,"sql_date":"2017-04-01T00:00:00"},
{"day":2,"sql_date":"2017-04-02T00:00:00"},
{"day":3,"sql_date":"2017-04-03T00:00:00"},
{"day":4,"sql_date":"2017-04-04T00:00:00"},
{"day":5,"sql_date":"2017-04-05T00:00:00"}];
var newArr = [];
for (var i in responseObj) {
newArr.push({"day":responseObj[i].day});
}
console.log(newArr);
Using Array map() method.
DEMO
var responseObj = [{"day":1,"sql_date":"2017-04-01T00:00:00"},
{"day":2,"sql_date":"2017-04-02T00:00:00"},
{"day":3,"sql_date":"2017-04-03T00:00:00"},
{"day":4,"sql_date":"2017-04-04T00:00:00"},
{"day":5,"sql_date":"2017-04-05T00:00:00"}];
var newArr = responseObj.map(function(item) {
return {"day":item.day};
});
console.log(newArr);
Using JavaScript for loop.
DEMO
var responseObj = [{"day":1,"sql_date":"2017-04-01T00:00:00"},
{"day":2,"sql_date":"2017-04-02T00:00:00"},
{"day":3,"sql_date":"2017-04-03T00:00:00"},
{"day":4,"sql_date":"2017-04-04T00:00:00"},
{"day":5,"sql_date":"2017-04-05T00:00:00"}];
var newArr = [];
for (var i = 0; i < responseObj.length; i++) {
newArr.push({"day": responseObj[i].day});
}
console.log(newArr);
Still you can use map instead of for loop. Please find the code snippet below
var arr = [{"day":1,"sql_date":"2017-04-01T00:00:00"},{"day":2,"sql_date":"2017-04-02T00:00:00"},{"day":3,"sql_date":"2017-04-03T00:00:00"},{"day":4,"sql_date":"2017-04-04T00:00:00"},{"day":5,"sql_date":"2017-04-05T00:00:00"}],
newArr = arr.map(function(obj) { return obj.day });
console.log(newArr);

Categories

Resources