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]

3 Answers3

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.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 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. – Redu Aug 26 '16 at 20:51
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);
Community
  • 1
  • 1
Hurricane Development
  • 2,449
  • 1
  • 19
  • 40
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/

Thalaivar
  • 23,282
  • 5
  • 60
  • 71