简体   繁体   English

组合两个具有完全相同结构的数组对象

[英]Combine two array objects with exactly the same structure

I am implementing a service that extracts a list of blacklist terms from a json file.我正在实施一项从 json 文件中提取黑名单术语列表的服务。

@Injectable()
export class BlacklistService {
  private readonly BLACKLIST_FOLDER = './assets/data/web-blacklist';
  private readonly blacklistEnglishTerms: any;
  private readonly blacklistFrenchTerms: any;

  constructor(
    public httpClient: HttpClient
  ) {
    this.blacklistEnglishTerms = this.httpClient.get(`${this.BLACKLIST_FOLDER}/en.json`);
    this.blacklistFrenchTerms = this.httpClient.get(`${this.BLACKLIST_FOLDER}/fr.json`);
  }

  public getAllBlackListTerms(): Observable<any> {
    return combineLatest([
      this.blacklistEnglishTerms,
      this.blacklistFrenchTerms
    ]);
  }
}

For reference, each json file looks like this:作为参考,每个 json 文件如下所示:

{
  "blacklist": [
    "...",
    "...",
    "..."
  ]
}

I retrieve all the items in my component as follows:我检索组件中的所有项目,如下所示:

this.blacklistService.getAllBlackListTerms().pipe(takeUntil(this.ngUnsubscribe)).subscribe(blacklistTerms => {
  console.log(blacklistTerms);
});

blacklistTerms returns as an array of 2 array objects. blacklistTerms作为 2 个数组对象的数组返回。 How do I combine these two objects into one object (both have the same structure).如何将这两个对象组合成一个 object(两者具有相同的结构)。

在此处输入图像描述

Given, you have known results, ie You know that the results of the bolth the Observbales would have only one key: blackList you can modify your service like:鉴于,您已经知道结果,即您知道 Observbales 的结果只有一个键:blackList 您可以修改您的服务,例如:

public getAllBlackListTerms(): Observable<any> {
    return zip(
        this.blacklistEnglishTerms,
        this.blacklistFrenchTerms
    ).pipe(map([first, second]) => {
        return { blackList: [...first.blackList, ...second.blackList]};
    });
}

I have also, replaced combineLatest() with zip() , because you probably want the results only when both has emitted values.我也将combineLatest()替换为zip() ,因为您可能只需要在两者都发出值时才需要结果。

Please try the following approach请尝试以下方法

 var s = [{a:[1,2,3]},{a:[6,7,8]}] var obj = {} for(let i in s){ if(;obj['a']){obj['a'] = s[i]['a'].} else{ obj['a'].push(...s[i]['a']) } } console.log(obj)

but here you need to manually define blacklist但是这里需要手动定义blacklist

Blacklist is an array - even a simple concat can help !!黑名单是一个数组 - 即使是简单的 concat 也可以提供帮助!

 var blacklistEnglishTerms = { "blacklist": [ "a", "b", "c" ] } var blacklistFrenchTerms = { "blacklist": [ "e", "f", "g" ] } console.log(blacklistEnglishTerms.blacklist.concat(blacklistFrenchTerms.blacklist));

i think you please use concat().我想你请使用 concat()。
the concat() is used for combine two arrays. concat() 用于组合两个 arrays。

 public getAllBlackListTerms(): Observable<any> { return combineLatest = this.blacklistEnglishTerms.concat(this.blacklistFrenchTerms); }

Try using flatMap() like blacklistTerms.flatMap(x => x.blacklist)尝试使用flatMap()blacklistTerms.flatMap(x => x.blacklist)

this.blacklistService.getAllBlackListTerms().pipe(takeUntil(this.ngUnsubscribe)).subscribe(blacklistTerms => {
  console.log(blacklistTerms.flatMap(x => x.blacklist));
});

Test is below.测试如下。

 let blacklistTerms = [ { blacklist: [0, 1, 2] }, { blacklist: [2, 3] } ]; console.log(blacklistTerms.flatMap(x => x.blacklist));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM