5

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?

4
  • 3
    How are you defining removing duplicates? Why is 0 in your result twice, is that not a duplicate? What have you tried? Commented Aug 11, 2016 at 10:49
  • only 2 zeroes are there in your expected Output.. Is that right? It supposed to have 3 zeroes isn't it? Commented Aug 11, 2016 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. Commented Aug 11, 2016 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? Commented Aug 11, 2016 at 11:00

4 Answers 4

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));

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

Comments

2

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

Comments

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

Comments

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);

1 Comment

I don't understand anything :( all the inline code and non explanatory variables makes its super hard to read

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.