Foreach over Object in Angular

I've got a little problem with my code. I already have a piece of code looping on an array (like this:) I take this array and turn it like this:

Here is the piece of code :

processPostData(data: any) {
    Array.prototype.forEach.call(data.resources, (element: any) => {
        console.log(element);
        Object.keys(element).forEach((key) => {
            console.log(key);
            if ((element[key] == null || element[key] == '') && ['process_limit_date', 'creation_date', 'closing_date', 'countAttachments', 'countNotes'].indexOf(key) === -1) {
                element[key] = this.lang.undefined;
            } else if (["senders","recipients"].indexOf(key) > 0) {
                if (element[key].length > 1) {
                    console.log(element[key]);
                    element[key] = this.lang.isMulticontact;
                } else {
                    element[key] = element[key][0];
                }
            } else if (key == 'status_icon' && element[key] == null) {
                element[key] = 'fa-question undefined';
            }

        });
    });

    return data;
}

Now the problem is that I modify my input "data" and now I have an object, like this : pic3

I have now an Object instead of an Array and I didn't manage to loop through it the same way before. How could I do that? At least, for the first loop, and get an Array, or an Object, with the content of the "23" and "24" indexes of the Object.

Tell me if I'm not clear enough, pretty hard to me to be as precise as possible

4

4 Answers

i am not sure if i understood you correctly, but you can iterate over objects like this:

for(let key in object) {
let child = object[key];
// Your Code
}

By default, object fields are not iterable in array-fashion way. You can however to try to wrap your handler in folliwing to be able to parse both array and object form:

if(Array.isArray(input)) {
  /* Do what you are already doing */
} else { /* We are working with object */
  Object.keys(input).forEach((key) => {
    /* Here you can iterate over each key in object */
  })
}

But still, your question scope is rather unclear

Be careful, I've noticed that you treat resource.senders and resource.recipients as array whereas it's a string in your screenshot.
I also noticed that if you have more than one sender/recipient you will assign this.lang.isMulticontact to so you won't have your senders/recipients anymore, is that what you want ?

Note that you should not modify your data parameter it can lead to undesirable side-effects.

processPostData(data) {
  const dataToProcess = [...data];
  dataToProcess.resources.forEach((resource) => {
    const propertiesUndefined = ['process_limit_date', 'creation_date', 'closing_date', 'countAttachments', 'countNotes'];
    propertiesUndefined.forEach((property) => {
      if (!resource[property]) { 
        resource[property] = this.lang.undefined;
      }
    });
    const propertiesMultiContact = ['senders', 'recipients'];
    propertiesMultiContact.forEach((property) => {
      if (resource[property] instance of Array && resource[property].length > 1) {
        resource[property] = this.lang.isMulticontact;
      }
    });
   if (!resource['status_icon']) {
     resource['status_icon'] = 'fa-question undefined';
   }
  });
  return dataToProcess;
}

Before you iterated through data.resources With your changes, you have to iterate through data.resources.docs Try something like that:

data.resources.forEach(resource => {
    console.log(resource);
    resource.docs.forEach(doc => {
        console.log(doc);
        // ...
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest