admin管理员组

文章数量:1327971

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?

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?

Share Improve this question asked Aug 11, 2016 at 10:47 malenmalen 951 silver badge4 bronze badges 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 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? – Rajaprabhu Aravindasamy 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. – malen 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? – Redu Commented Aug 11, 2016 at 11:00
Add a ment  | 

4 Answers 4

Reset to default 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));

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

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

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

本文标签: Mergeflatten multidimensional array and remove duplicates JavascriptStack Overflow