0

I'm beginner in JavaScript, recently I got this problem. I have an array -

var array_input = [1,2, [1,2,3], [4,5,6, [9,8,10]]];

and I want out of the following array same as below (need answer using for loop):

var array_output = [1,2,3,4,5,6,7,8,9,10]
7
  • 1
    and what have you tried until now? Commented Aug 26, 2016 at 14:52
  • I'm beginner and don't have any idea... Commented Aug 26, 2016 at 14:53
  • 1
    this is the flattening part: stackoverflow.com/questions/10865025/… Commented Aug 26, 2016 at 14:55
  • this is the sorting part: stackoverflow.com/questions/979256/… Commented Aug 26, 2016 at 14:56
  • 1
    Is there a reason the number 7 appears in the result? There's no real question here. You're just telling people what you "want". Commented Aug 26, 2016 at 15:14

3 Answers 3

2

You could use a Set for this, together with a recursive function to flatten the result, and then finally apply a basic sort on it:

function getValues(arr) {
  return [...arr.reduce( (acc, val) => 
    new Set([...acc, ...(Array.isArray(val) ? getValues(val) : [val])]), [])];
}
// Sample input
var array_input = [2,1, [1,3,2], [4,5,6, [9,8,10]]];
// Get values and sort them
var result = getValues(array_input).sort( (a,b) => a-b );
// Show output
console.log(result);

Object keys alternative

The same principle can be used with an object using the values as keys, although I prefer the Set way. As object keys are iterated in numerical order (when they are numerical), the explicit sorting step is not needed here:

function getValuesAsKeys(arr) {
  return arr.reduce( (acc, val) => 
    Object.assign(acc, Array.isArray(val) ? getValuesAsKeys(val) : { [val]: val }), {} );
}

// Sample input
var array_input = [2,1, [1,3,2], [4,5,6, [9,8,10]]];
// Get values and sort them
var result = Object.values(getValuesAsKeys(array_input));
// Show output
console.log(result);

NB: The Object.values method has at this moment little support. Object.keys(getValues(array_input)).map(Number) can be used instead as in this object the keys are the values, but in string type (of course).

Number-String-Number alternative

Converting the array to string is another possibility, but I don't really like to perform conversion when in the end you need the numbers, not strings.

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

1 Comment

When i write code like this (i mean snippet 1) i get criticized as my code being not maintainable yet i think it fully is. Your solution is nice but on the other hand the question is not clear on how to handle the duplicates at the same level... as in should they also be removed or not. Anyways... i have decided to up-vote people writing code as it should be written.
1

Ok I am using a bunch of things form different sources. First to condense the array, @Thalaivar's answer nails it.

var array_input = [1,2, [1,2,3], [4,5,6, [9,8,10]]];
var pArray = array_input.join().split(',').map(Number);

Then, using this answer ad some slight modification (implement a number sort), we will remove any duplicates form the array while sorting it:

function uniqSort(a) {
    return a.sort(function sortNumber(a,b) {
        return a - b;
    }).filter(function(item, pos, ary) {
        return !pos || item != ary[pos - 1];
    })
}

Then just call that function.

var array_output = uniqSort(pArray);

Comments

0
var array_input = [1,2, [1,2,3], [4,5,6, [9,8,10]]];
console.log(array_input.join().split(',').map(Number));

You could use the above... https://jsfiddle.net/vp9kq3a8/

1 Comment

Wow i had no idea join is recursive like that. Thanks!

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.