1

I am iterating over a collection of data, in my case, an array of objects. Here is a sample of 2 data points from it:

 {
violation_id: '211315',
inspection_id: '268804',
violation_category: 'Garbage and Refuse',
violation_date: '2012-03-22 0:00',
violation_date_closed: '',
violation_type: 'Refuse Accumulation' },

{
violation_id: '214351',
inspection_id: '273183',
violation_category: 'Building Conditions',
violation_date: '2012-05-01 0:00',
violation_date_closed: '2012-04-17 0:00',
violation_type: 'Mold or Mildew' }

I need to create a new array of objects from this, one for each "violation_category" property. If Violation category already exists in the new array I am creating, i simply add the information to that existing category object (instead of having two "building conditions" objects for example, I would just add to an existing one).

However, I am having trouble assigning to the existing object if the current one exists (it's easy to check if it does not, but not the other way around). This is what am attempting to do currently:

    if (violationCategory.uniqueCategoryName) {
  violationCategory.uniqueCategoryName.violations = results[i].violation_id;
  violationCategory.uniqueCategoryName.date = results[i].violation_date;
  violationCategory.uniqueCategoryName.closed =
    results[i].violation_date_closed;
} else {
  category.violations = results[i].violation_id;
  category.date = results[i].violation_date;
  category.closed = results[i].violation_date_closed;
  violationCategory.push(category);
}

In first condition, if this category (key) exists, I simply add to it, and in the second condition, this is where I am struggling. Any help appreciated. Thanks guys.

1
  • do you maybe want group by violation_category? Commented Aug 12, 2019 at 9:49

2 Answers 2

1

Just add an empty object to the key if there no object there :

violationCategory.uniqueCategoryName = violationCategory.uniqueCategoryName || {};

And only then, add the data you want to the object.

   violationCategory.uniqueCategoryName.violations = results[i].violation_id;
   violationCategory.uniqueCategoryName.date = results[i].violation_date;
   violationCategory.uniqueCategoryName.closed =
   results[i].violation_date_closed;

No condition needed.

Good luck!

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

3 Comments

Thanks buddy, if I wanted the "category name" to exist as a key in the object, itself holding an object with the date, id, and closed value...do you have any idea how I could do that?
@TomK I Don't sure what are you asking about. you want uniqueCategoryName to be an variable? if so you can access the object with a variable like so: violationCategory[uniqueCategoryName].whatEverSubKeyYouWant = 'value';
it's ok I figured it out =)
1

Assuming that you have an input variable which is an array of objects, where the objects are looking like the objects of the question, you can generate your output like this:

var output = {};
for (var item of input) {
    if (!output[item.violation_category]) output[item.violation_category] = [];
    output[item.violation_category].push(item);
}

Of course you might customize it like you want.

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.