4

I have a multidimensional array that looks like this:

var workouts = [
  [0, Object, 0, 0, 0],
  [Object, 0, 0, 0, 0],
  [0, 0, 0, Object, 0]
];

I'd like to flatten/merge the array and remove the duplicates. The result should look something like this:

[Object, Object, 0, Object, 0]

Is it possible to perform?

malen
  • 85
  • 1
  • 4
  • 3
    How are you defining removing duplicates? Why is 0 in your result twice, is that not a duplicate? What have you tried? – UnknownFury Aug 11 '16 at 10:49
  • only 2 zeroes are there in your expected Output.. Is that right? It supposed to have 3 zeroes isn't it? – Rajaprabhu Aravindasamy Aug 11 '16 at 10:51
  • Yes, it should be 2 zeros in the expected output. My plan is to loop the expected output and then render different elements depending on the 0 / the object. – malen Aug 11 '16 at 10:55
  • Are you mapping the indices of inner arrays and mark the ones with the object if an inner array has an onbect at that index or 0 if not? – Redu Aug 11 '16 at 11:00

4 Answers4

2

My understanding is that you want to keep the first encountered object in a given column if it exists.

You can do that with .map() and .reduce():

var workouts = [
  [0, {id:1}, 0, 0, 0],
  [{id:2}, 0, 0, 0, 0],
  [0, 0, 0, {id:3}, 0]
];

var res = workouts.reduce(function(a, b) {
  return b.map(function(e, i) { return a[i] instanceof Object ? a[i] : e; });
}, []);


console.log(JSON.stringify(res));
Arnauld
  • 5,847
  • 2
  • 15
  • 32
1

Late for the party, but I still want to make use of:

  • Array.prototype.flat() for reducing the dimension to 1,
  • Set for removing dupes
  • Spread Syntax for getting array out of that set.
  • JSON.stringify and parse back for handling dupes of Objects.

Here's the code and a very inspirational posts 1 and 2:

const myArray = [
  [0, {id:1}, 0, 0, 0],
  [{id:2}, 0, 0, 0, 0],
  [0, 0, 0, {id:2}, 0]
]

const myFlatArray = myArray.flat()
const result = [...new Set(myFlatArray.map(JSON.stringify))].map(JSON.parse)

console.log(JSON.stringify(result))

Note: ES6 must be supported for this solution

vahdet
  • 6,357
  • 9
  • 51
  • 106
0
var workouts = [
  [0, {id:1}, 0, 0, 0],
  [{id:2}, 0, 0, 0, 0],
  [0, 0, 0, {id:3}, 0]
];

function flattenArray (array){
  var newArray = [];
  for (i=0; i<array.length; i++){
    var subArray = array[i];
    for (ii=0; ii<subArray.length; ii++){
      if (newArray.indexOf(subArray[ii]) == -1){
      newArray.push(subArray[ii]);
      }
    }
  }
  return newArray;
}
console.log(flattenArray(workouts));

http://codepen.io/anon/pen/WxLrqL

charsi
  • 2,917
  • 22
  • 40
0

I would do like this;

var workouts = [[0, {a:1}, 0, 0, 0],
                [{b:2}, 0, 0, 0, 0],
                [0, 0, 0, {c:3}, 0]
               ],
      result = workouts.reduce((p,c) => p = c.map((e,i) => e || p[i] || 0 ),[])
console.log(result);
Redu
  • 25,060
  • 6
  • 56
  • 76
  • 3
    I don't understand anything :( all the inline code and non explanatory variables makes its super hard to read – Piotr Kula Aug 07 '18 at 09:09