0

I have a data object model like the following:

export class JournalEntries {
  id?: number;
  year?: string;
  month: string;
  link?: string;
  months?: any;
}

A sample data would be:

"journalentries": [
    { 
      "id": "2000001",
      "year": "2021", 
      "months": [
        {
          "year": "2021", 
          "month": "January", 
          "link": "Jan_Feb_2021"
        },
        {
          "year": "2021", 
          "month": "February",
          "link": "Jan_Feb_2021"
        },
         ...
        {
          "year": "2021", 
          "month": "December",
          "link": ""
        }
      ]
    },
    { 
      "id": "2000002",
      "year": "2020", 
      "months": [
        {
          "year": "2020", 
          "month": "January", 
          "link": "Jan_Feb_2020"
        },
        {
          "year": "2020", 
          "month": "February",
          "link": "Jan_Feb_2020"
        },
        ...
        {
          "year": "2020", 
          "month": "December",
          "link": "Dec_2020"
        }
      ]
    },
 ]

My problem is, I can't seem to add a new month data to the nested array in the appropriate year. So if I add 2021, May, and some_link in the text boxes in the HTML part, it doesnt add it to the nested array. it adds it to the Main array. Which makes sense because this is what I have in the saveJournals click event

  this.journals.unshift( //putting it on the journals array
    {
      year: this.selectedRowData.year,
      month: this.selectedRowData.month,
      link: this.selectedRowData.link
    }
  )

So I get why it's doing that. But the following is coming up with an error

  this.journals.months.unshift( //Same with this.journals[].months.unshift
    {
      year: this.selectedRowData.year,
      month: this.selectedRowData.month,
      link: this.selectedRowData.link
    }
  )

I understand this could be a combination of problems, but I'm hoping someone knows the solution even with the vague data.

My expected goal is, if year already exist, add (year, month, link) to its months array else create year with new months array. Shooot! I'd be happy if you can help me with the unshift part only!

5
  • 1
    Well, you really should type months to be { year: string, month: string, link: string }[] rather than any, but I digress. What we need to know is how does this.journals match up with journalentries in your data? Perhaps you could help by putting together a minimal reproducible example using Stack Snippets (icon is <> in the toolbar)? Commented Jun 29, 2021 at 19:10
  • I tried. There are some many dependencies and moving parts. But I can try. The model and data match is in my OP. I can manage the delete and edit. But I can't add for the life of me Commented Jun 29, 2021 at 19:14
  • Note the "minimal" part there; for this question, you probably don't need Angular, for instance. Commented Jun 29, 2021 at 19:18
  • can you share the error message please? Commented Jun 29, 2021 at 19:18
  • @MarikIshtar Property 'months' does not exist on type 'JournalEntries[]'. But if I add [0] as you suggested it spushes it to the first array only. So the data transfer and model is working. It's the array handling that I am not figuring out Commented Jun 29, 2021 at 19:36

2 Answers 2

1

In this solution, I loop through the journal entries until I find the year I am looking for, then I modify the existing entry using unshift to prepend the selected month and link.

const journalentries = [
  { 
    "id": "2000001",
    "year": "2021", 
    "months": [
      {
        "year": "2021", 
        "month": "January", 
        "link": "Jan_Feb_2021"
      },
      {
        "year": "2021", 
        "month": "February",
        "link": "Jan_Feb_2021"
      },
      {
        "year": "2021", 
        "month": "December",
        "link": ""
      }
    ]
  },
  { 
    "id": "2000002",
    "year": "2020", 
    "months": [
      {
        "year": "2020", 
        "month": "January", 
        "link": "Jan_Feb_2020"
      },
      {
        "year": "2020", 
        "month": "February",
        "link": "Jan_Feb_2020"
      },
      {
        "year": "2020", 
        "month": "December",
        "link": "Dec_2020"
      }
    ]
  },
];

const selectedYear = '2021';
const selectedMonth = 'May';
const selectedLink = 'May_Aug_2021';
journalentries.forEach(entry => {
  if (entry.year==selectedYear) {
    entry.months.unshift({
      year: selectedYear,
      month: selectedMonth,
      link: selectedLink
    });
  }
});
console.log(journalentries);

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

1 Comment

BOOYAHHH!!!! You nailed it! Now I just have to create the year if it doesn't exist hahaha. Buy yourself a beer on me!
1

Check this, I hope it helps you somehow:

const journalentries = [
    { 
      "id": "2000001",
      "year": "2021", 
      "months": [
        {
          "year": "2021", 
          "month": "January", 
          "link": "Jan_Feb_2021"
        },
        {
          "year": "2021", 
          "month": "February",
          "link": "Jan_Feb_2021"
        },
        {
          "year": "2021", 
          "month": "December",
          "link": ""
        }
      ]
    },
    { 
      "id": "2000002",
      "year": "2020", 
      "months": [
        {
          "year": "2020", 
          "month": "January", 
          "link": "Jan_Feb_2020"
        },
        {
          "year": "2020", 
          "month": "February",
          "link": "Jan_Feb_2020"
        },
        {
          "year": "2020", 
          "month": "December",
          "link": "Dec_2020"
        }
      ]
    },
 ]
 
 journalentries[0].months.unshift({
          "year": "2077", 
          "month": "December",
          "link": "Dec_2077"
        })
        
console.log("journalentries >> ", journalentries)

3 Comments

The OP wants to be able to add to any year. Your code will only add to the first year in the array of journalEntries.
As daddygames said, this works to the first year only, though it is not entriely bad because these are journals that get added every month and hardly ever get added to prior years, but it could happen.
What would be swell is if somehow the array position could be dynamic to recognize which year I want to unshift it to.

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.