我有两个数组
a = [1, 2, 3, 4, 5]
b = [2, 4, 6]
我想合并两个数组,然后删除与其他数组相同的值。结果应该是:
c = [1, 3, 5, 6]
我尝试减去两个数组,结果是[1,3,5]。我还想从第二个数组获取第一个数组中没有重复的值..
答案 0 :(得分:24)
使用Array#uniq
。
a = [1, 3, 5, 6]
b = [2, 3, 4, 5]
c = (a + b).uniq
=> [1, 3, 5, 6, 2, 4]
答案 1 :(得分:14)
您可以执行以下操作!
# Merging
c = a + b
=> [1, 2, 3, 4, 5, 2, 4, 6]
# Removing the value of other array
# (a & b) is getting the common element from these two arrays
c - (a & b)
=> [1, 3, 5, 6]
尽管我独立提出了自己的想法,但Dmitri的评论也是一样的。
答案 2 :(得分:12)
这个怎么样。
events = this.props.schedule.map(function(event) {
return (
<li className="animated" key={event.time}>
<ScheduleEventItem event={event} />
</li>
);
});
return (
<React.addons.CSSTransitionGroup className="animated"
transitionName={{enter: "flipInX", leave: "flipOutX"}}
transitionEnterTimeout={1000} transitionLeaveTimeout={1}
>
{events}
</React.addons.CSSTransitionGroup>
);
答案 3 :(得分:3)
答案 4 :(得分:0)
Set.new([1,2,3]+[1,4,5])
怎么样?
返回[1,2,3,4,5]
答案 5 :(得分:0)
Let's have two array
p = [1, 2, 5, 4, 8, 9]
q = [5, 6, 4, 8, 5, 3]
(p+q).uniq or (p.concat(q)).uniq
=> [1, 2, 5, 4, 8, 9, 6, 3]
Also p|q
can do the job! Decide which one suits for you.