如何将两个数组交叉连接成一个?

时间:2018-05-23 09:27:08

标签: arrays rxjs

对于两个长度相同的数组,使用哪些运算符来交叉连接它们?

The country table's fields:
    ctry_id;
    ctry_name;
    ctry_flag;
    ctry_president.

The city table's fields:
    cty_id;
    cty_ctry_id; (country id)
    cty_name.

如何使用RxJS交叉加入它们来获取:

name = ['tom', 'jerry', 'pete'];
age = [4, 5, 6];

谢谢!

2 个答案:

答案 0 :(得分:0)

假设数组nameage都是observable,您可以使用.zip运算符:

Observable.zip(name,age,([named,aged])=>`{named}{aged}`)

如果您希望结果位于数组中,可以进一步reduce

Observable.zip(name, age, ([named, aged]) => `{named}{aged}`)
    .reduce((acc, x) => {
        return acc.push(x)
    }, [])

答案 1 :(得分:0)

没关系,使用起来非常简单:

zip(from(name), from(age), (n: string, a: number) => 
    (n + String(a)))
.subscribe(val => console.log('zip output: ' + val));