7

I am using a function in a JavaScript framework where the return value can be ANY of the following

  1. a single xy coordinate pair

    [x,y]
    
  2. an array of xy coordinate pairs

    [[x,y],[x,y],...]
    
  3. an array of arrays of xy coordinate pairs

    [[[x,y],[x,y]],[[x,y],[x,y]],...]
    

The return value depends on the geometry of the object (single point, line, or multiple lines). Regardless of the return value and its array depth, I want to grab the first xy coordinate pair. What is an efficient way to do this?

Here is my code to achieve the objective so far:

//here is the magic method that can return one of three things :)
var mysteryCoordinates = geometry.getCoordinates();
var firstCoord;

if(typeof mysteryCoordinates[0] === 'number') {
    firstCoord = mysteryCoordinates;
} else if (typeof mysteryCoordinates[0][0] === 'number') {
    firstCoord = mysteryCoordinates[0];
} else if (typeof mysteryCoordinates[0][0][0] === 'number') {
    firstCoord = mysteryCoordinates[0][0];
}

I really hate this solution and am looking for something a bit more elegant.

4
  • 2
    Does the geometry object not have some property telling you what type of geometry it represents, ie point,line,lines? Commented Sep 28, 2016 at 18:46
  • 1
    I want to grab the first xy coordinate pair are you interested only on the first pair ?? Or later you might want some other index data as well? Commented Sep 28, 2016 at 18:47
  • @PatrickEvans It does, but I would have to do three separate typeof checks on the geometry object to know the proper depth to access the first coordinate pair. Commented Sep 28, 2016 at 18:59
  • @Reddy Yes, I am only interested in the first pair. Commented Sep 28, 2016 at 19:00

2 Answers 2

4

I guess in pure JS this should do it;

var    arr = [[[1,2],[1,3]],[[4,8],[3,9]]],
getFirstXY = a => Array.isArray(a[0]) ? getFirstXY(a[0]) : a;

console.log(getFirstXY(arr));

Sign up to request clarification or add additional context in comments.

5 Comments

you need no concat.
@Nina Scholz Cool..! Thanks :)
@Redu This works for all 3 scenarios. Care to explain what is going on?
@JellyRaptor getFirstXY is a recursive function. It checks for the first item of the supplied array and if it is an array calls getFirstXY again with a[0] being the argument up until it is not and then returns the argument array.
@Redu This is pretty awesome. Answer accepted. Thanks!
2

A less efficient, but more elegant solution would be to use _.flatten (http://underscorejs.org/#flatten):

let firstCoord = _.flatten(mysteryCoordinates).slice(0, 2);

You could make it a little more efficient on average by slicing off the first two elements up-front as well:

let firstCoord = _.flatten(mysteryCoordinates.slice(0, 2)).slice(0, 2);

console.log(_.flatten([1,2]).slice(0, 2));
console.log(_.flatten([[1,2],[1,3],[4,8],[3,9]]).slice(0, 2));
console.log(_.flatten([[[1,2],[1,3]],[[4,8],[3,9]]]).slice(0, 2));
<script src="http://underscorejs.org/underscore-min.js"></script>

3 Comments

Upon further review, this solution only works for scenarios 1 and 2, but not 3. It spits out an array of pairs when the input is the array of arrays of pairs.
Seems to work for me with the latest version of underscore. Maybe you accidentally passed a truthy value for shallow?
+1 for demonstrating functionality. I initially checked results using the flatten() function from the latest lodash version. I presumed it would be equivalent to underscore's flatten, but evidently it is not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.