JS: writing a function that has an input of a multidimensional array
up vote
-3
down vote
favorite
I want to input an array of arrays, but then pick them apart. so for example I have an array of locations so like multiple coordinates of latitude and longitude.
but I want to write a loop that will then take that array of arrays and make and array for all of the latitude coordinates and all of the longitude coordinates.
so for example if I had
input = [[45,45],[35,75][85,90]]
it would make 2 arrays as my output
[45,35,85]
and
[45,75,90]
javascript arrays
add a comment |
up vote
-3
down vote
favorite
I want to input an array of arrays, but then pick them apart. so for example I have an array of locations so like multiple coordinates of latitude and longitude.
but I want to write a loop that will then take that array of arrays and make and array for all of the latitude coordinates and all of the longitude coordinates.
so for example if I had
input = [[45,45],[35,75][85,90]]
it would make 2 arrays as my output
[45,35,85]
and
[45,75,90]
javascript arrays
4
What is your your question? Where are you having problems?
– slider
Nov 8 at 18:23
What you are looking for is a thing calledzip
, orzipWith
– bugs
Nov 8 at 18:23
1
Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
– Andy
Nov 8 at 18:27
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I want to input an array of arrays, but then pick them apart. so for example I have an array of locations so like multiple coordinates of latitude and longitude.
but I want to write a loop that will then take that array of arrays and make and array for all of the latitude coordinates and all of the longitude coordinates.
so for example if I had
input = [[45,45],[35,75][85,90]]
it would make 2 arrays as my output
[45,35,85]
and
[45,75,90]
javascript arrays
I want to input an array of arrays, but then pick them apart. so for example I have an array of locations so like multiple coordinates of latitude and longitude.
but I want to write a loop that will then take that array of arrays and make and array for all of the latitude coordinates and all of the longitude coordinates.
so for example if I had
input = [[45,45],[35,75][85,90]]
it would make 2 arrays as my output
[45,35,85]
and
[45,75,90]
javascript arrays
javascript arrays
asked Nov 8 at 18:21
Zach Lyness SonicEX
11
11
4
What is your your question? Where are you having problems?
– slider
Nov 8 at 18:23
What you are looking for is a thing calledzip
, orzipWith
– bugs
Nov 8 at 18:23
1
Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
– Andy
Nov 8 at 18:27
add a comment |
4
What is your your question? Where are you having problems?
– slider
Nov 8 at 18:23
What you are looking for is a thing calledzip
, orzipWith
– bugs
Nov 8 at 18:23
1
Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
– Andy
Nov 8 at 18:27
4
4
What is your your question? Where are you having problems?
– slider
Nov 8 at 18:23
What is your your question? Where are you having problems?
– slider
Nov 8 at 18:23
What you are looking for is a thing called
zip
, or zipWith
– bugs
Nov 8 at 18:23
What you are looking for is a thing called
zip
, or zipWith
– bugs
Nov 8 at 18:23
1
1
Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
– Andy
Nov 8 at 18:27
Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
– Andy
Nov 8 at 18:27
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
You could transpose the array and take lat
and long
as single arrays.
var input = [[45, 45], [35, 75], [85, 90]],
[lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );
console.log(lat);
console.log(long);
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
up vote
-1
down vote
Try this, loops over every coordinate and puts the first value from each coordinate into the array called first
and the second value from each coordinate into an array called second
..
var input = [[45,45],[35,75],[85,90]];
function splitValues(coordinates) {
var first = ;
var second = ;
for (var i = 0; i < coordinates.length; i++) {
first.push(coordinates[i][0]);
second.push(coordinates[i][1]);
}
}
splitValues(input);
add a comment |
up vote
-1
down vote
This will help assuming you will always have a 2 values array in input and you want only 2 results
const array = [[45,45],[35,75],[85,90]]
let first =
let second =
array.forEach((item)=>{
first.push(item[0])
second.push(item[1])
})
console.log(first)
console.log(second)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You could transpose the array and take lat
and long
as single arrays.
var input = [[45, 45], [35, 75], [85, 90]],
[lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );
console.log(lat);
console.log(long);
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
up vote
0
down vote
You could transpose the array and take lat
and long
as single arrays.
var input = [[45, 45], [35, 75], [85, 90]],
[lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );
console.log(lat);
console.log(long);
.as-console-wrapper { max-height: 100% !important; top: 0; }
add a comment |
up vote
0
down vote
up vote
0
down vote
You could transpose the array and take lat
and long
as single arrays.
var input = [[45, 45], [35, 75], [85, 90]],
[lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );
console.log(lat);
console.log(long);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You could transpose the array and take lat
and long
as single arrays.
var input = [[45, 45], [35, 75], [85, 90]],
[lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );
console.log(lat);
console.log(long);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var input = [[45, 45], [35, 75], [85, 90]],
[lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );
console.log(lat);
console.log(long);
.as-console-wrapper { max-height: 100% !important; top: 0; }
var input = [[45, 45], [35, 75], [85, 90]],
[lat, long] = input.reduce((r, a) => a.map((v, i) => (r[i] || ).concat(v)), );
console.log(lat);
console.log(long);
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered Nov 8 at 18:44
Nina Scholz
172k1384147
172k1384147
add a comment |
add a comment |
up vote
-1
down vote
Try this, loops over every coordinate and puts the first value from each coordinate into the array called first
and the second value from each coordinate into an array called second
..
var input = [[45,45],[35,75],[85,90]];
function splitValues(coordinates) {
var first = ;
var second = ;
for (var i = 0; i < coordinates.length; i++) {
first.push(coordinates[i][0]);
second.push(coordinates[i][1]);
}
}
splitValues(input);
add a comment |
up vote
-1
down vote
Try this, loops over every coordinate and puts the first value from each coordinate into the array called first
and the second value from each coordinate into an array called second
..
var input = [[45,45],[35,75],[85,90]];
function splitValues(coordinates) {
var first = ;
var second = ;
for (var i = 0; i < coordinates.length; i++) {
first.push(coordinates[i][0]);
second.push(coordinates[i][1]);
}
}
splitValues(input);
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Try this, loops over every coordinate and puts the first value from each coordinate into the array called first
and the second value from each coordinate into an array called second
..
var input = [[45,45],[35,75],[85,90]];
function splitValues(coordinates) {
var first = ;
var second = ;
for (var i = 0; i < coordinates.length; i++) {
first.push(coordinates[i][0]);
second.push(coordinates[i][1]);
}
}
splitValues(input);
Try this, loops over every coordinate and puts the first value from each coordinate into the array called first
and the second value from each coordinate into an array called second
..
var input = [[45,45],[35,75],[85,90]];
function splitValues(coordinates) {
var first = ;
var second = ;
for (var i = 0; i < coordinates.length; i++) {
first.push(coordinates[i][0]);
second.push(coordinates[i][1]);
}
}
splitValues(input);
answered Nov 8 at 18:26
bobbyrne01
2,09343891
2,09343891
add a comment |
add a comment |
up vote
-1
down vote
This will help assuming you will always have a 2 values array in input and you want only 2 results
const array = [[45,45],[35,75],[85,90]]
let first =
let second =
array.forEach((item)=>{
first.push(item[0])
second.push(item[1])
})
console.log(first)
console.log(second)
add a comment |
up vote
-1
down vote
This will help assuming you will always have a 2 values array in input and you want only 2 results
const array = [[45,45],[35,75],[85,90]]
let first =
let second =
array.forEach((item)=>{
first.push(item[0])
second.push(item[1])
})
console.log(first)
console.log(second)
add a comment |
up vote
-1
down vote
up vote
-1
down vote
This will help assuming you will always have a 2 values array in input and you want only 2 results
const array = [[45,45],[35,75],[85,90]]
let first =
let second =
array.forEach((item)=>{
first.push(item[0])
second.push(item[1])
})
console.log(first)
console.log(second)
This will help assuming you will always have a 2 values array in input and you want only 2 results
const array = [[45,45],[35,75],[85,90]]
let first =
let second =
array.forEach((item)=>{
first.push(item[0])
second.push(item[1])
})
console.log(first)
console.log(second)
answered Nov 8 at 18:27
Exequiel Aguirre
56527
56527
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53213912%2fjs-writing-a-function-that-has-an-input-of-a-multidimensional-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
What is your your question? Where are you having problems?
– slider
Nov 8 at 18:23
What you are looking for is a thing called
zip
, orzipWith
– bugs
Nov 8 at 18:23
1
Welcome to SO. You might find reading the site help section useful when it comes to asking a good question. To get the best answers to your question we like to see a) that you've attempted to solve the problem yourself first, and b) used a Minimal, Complete, and Verifiable example to narrow down the problem. Asking SO to do all the work for you doesn't help you or us. Here's a question checklist you might find useful..
– Andy
Nov 8 at 18:27