0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

test

0
Last updated at Posted at 2025-07-22
// 共通メソッド(OldListのキー名を指定してリストを再構築)
rebuildListByRegistDateDynamic(list: any[], listPropName: string): any[] {
  const allNestedItems: any[] = [];

  // すべての OldList をまとめる(termsOldListやmovieOldList)
  for (const item of list) {
    if (Array.isArray(item[listPropName])) {
      for (const nested of item[listPropName]) {
        allNestedItems.push({ ...nested });
      }
    }
  }

  // registDate(12桁)で一致する nested を再構築
  return list.map(item => {
    const registDate12 = item.registDate.substring(0, 12);

    const matchedList = allNestedItems
      .filter(nested => nested.registDate.substring(0, 12) === registDate12)
      .map(nested => ({
        ...nested,
        registDate: nested.registDate.substring(0, 12),
      }));

    return {
      ...item,
      registDate: registDate12,
      [listPropName]: matchedList
    };
  });
}

ngOnInit(): void {
  const updatedTermList = this.rebuildListByRegistDateDynamic(
    postData.termList,
    'termsOldList'
  );

  const updatedMovieList = this.rebuildListByRegistDateDynamic(
    postData.movieList,
    'movieOldList'
  );

  console.log('updatedTermList:', updatedTermList);
  console.log('updatedMovieList:', updatedMovieList);
}



mergeByRegistDate(
  updatedTermList: any[],
  updatedMovieList: any[]
): any[] {
  const mergedMap = new Map<string, any>();

  const mergeItem = (item: any, type: 'term' | 'movie') => {
    const registDate = item.registDate;

    if (!mergedMap.has(registDate)) {
      mergedMap.set(registDate, {
        registDate,
        OldList: [],
      });
    }

    const existing = mergedMap.get(registDate);

    // マージ(重複フィールドも含めて上書きOK:term → movie の順)
    for (const key of Object.keys(item)) {
      if (key !== 'registDate') {
        existing[key] = item[key];
      }
    }

    // OldList の統合
    const listKey = type === 'term' ? 'termsOldList' : 'movieOldList';
    if (item[listKey]) {
      existing[listKey] = item[listKey];
      existing.OldList.push(...item[listKey]);
    }
  };

  // term → movie の順でマージ
  updatedTermList.forEach(item => mergeItem(item, 'term'));
  updatedMovieList.forEach(item => mergeItem(item, 'movie'));

  // registDate 昇順で返す
  return Array.from(mergedMap.values()).sort((a, b) =>
    a.registDate.localeCompare(b.registDate)
  );
}


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?