我想将这些数组合并到具有唯一值的单个数组中。 不要重复值。
0:Array(2) ["General Fitness", "Health & Fitness"]
1:Array(2) ["Cardiovascular", "Meditation"]
2:Array(3) ["Health & Fitness", "Cardiovascular", "General Fitness"]
3:Array(1) ["Cardiovascular"]
我期待这个答案:
["General Fitness", "Health & Fitness", "Cardiovascular", "Meditation"]
答案 0 :(得分:3)
使用lodash,您可以使用_.union()
和_.spread()
代替Set和JS传播:
var arrs = [
["General Fitness", "Health & Fitness"],
["Cardiovascular", "Meditation"],
["Health & Fitness", "Cardiovascular", "General Fitness"],
["Cardiovascular"]
];
var result = _.spread(_.union)(arrs);
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
&#13;
使用ES6,您可以concat数组,并通过将Set和spreading传递回数组来使结果唯一:
const arrs = [
["General Fitness", "Health & Fitness"],
["Cardiovascular", "Meditation"],
["Health & Fitness", "Cardiovascular", "General Fitness"],
["Cardiovascular"]
];
const result = [...new Set([].concat(...arrs))];
console.log(result);
&#13;
答案 1 :(得分:3)
const arrs = [
["General Fitness", "Health & Fitness"],
["Cardiovascular", "Meditation"],
["Health & Fitness", "Cardiovascular", "General Fitness"],
["Cardiovascular"]
];
const result = _.uniq(_.flatten(arrs));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
答案 2 :(得分:1)
使用union
var object = ["General Fitness", "Health & Fitness"];
var other = ["Cardiovascular", "Health & Fitness", "Meditation"]
_.union(object, other);
// ["General Fitness", "Health & Fitness", "Cardiovascular", "Meditation"]
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>