0

I am implementing a service that extracts a list of blacklist terms from a json file.

@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:

{
  "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. How do I combine these two objects into one object (both have the same structure).

enter image description here

2
  • You may check out this one Commented May 4, 2020 at 10:23
  • Try with blacklistTerms.flatMap(x => x.blacklist) Commented May 4, 2020 at 10:25

5 Answers 5

2

Given, you have known results, i.e. You know that the results of the bolth the Observbales would have only one key: blackList you can modify your service like:

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

Blacklist is an array - even a simple concat can help !!

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

Comments

2

i think you please use concat().
the concat() is used for combine two arrays.

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

Comments

1

Try using flatMap() like 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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.