0

I'm trying to iterate this block of code for displaying data in table. I want object arrays of equal length.

Hence, need to fill undefined values for respective keys to make array of objects uniform (same length)

Original Json

[
  {
    "toolName": "Alteryx",
    "contacts": [
      {
        "contactPerson": "James clear",
        "email": "[email protected]"
      },
      {
        "contactPerson": "Paul Unger",
        "email": "[email protected]"
      }
    ]
  },
  {
    "toolName": "Processes",
    "contacts": [
      {
        "contactPerson": "naomi Unger",
        "email": "[email protected]"
      }
    ]
  },
  {
    "toolName": "Alteryx Server",
    "contacts": [
      {
        "contactPerson": "Avinash",
        "email": "[email protected]"
      },
      {
        "contactPerson": "Sowmia",
        "email": "[email protected]"
      }
    ]
  }
]

Expectation json

[
  {
    "toolName": "Alteryx",
    "contacts": [
      {
        "contactPerson": "James clear",
        "email": "[email protected]"
      },
      {
        "contactPerson": "Paul Unger",
        "email": "[email protected]"
      }
    ]
  },
  {
    "toolName": "Processes",
    "contacts": [
      {
        "contactPerson": "naomi Unger",
        "email": "[email protected]"
      },
    {
        "contactPerson": null,
        "email": null
      }
    ]
  },
  {
    "toolName": "Alteryx Server",
    "contacts": [
      {
        "contactPerson": "Avinash",
        "email": "[email protected]"
      },
      {
        "contactPerson": "Sowmia",
        "email": "[email protected]"
      }
    ]
  }
]

Tried this, but not working

let max = 0;
  const masterArray = res?.data?.tools?.map((obj) => {
    obj.contacts.forEach((ele, ind) => {
      if(max <= ind){
                max = ind;
      }
      for(let i = 0 ; i< max; i++){
        if (ele !== undefined) return ele;
         return { ...ele,
           contactPerson: '',
           email: '',
         }
      }
    });
    
  });

How to fill null/undefined values to handle error.

1
  • 1
    My instinct is to leave the data the same and deal with this when rendering if possible. Easy to use some default value if you discover something missing. const contact = el.contacts[i] ?? {contactPerson: null, email: null}; Commented Nov 22, 2022 at 15:29

3 Answers 3

1

Only need to get max once.

function fillMissingContacts(arr) {
    const max = arr.reduce((max, el) =>
        Math.max(max, el.contacts.length), 0);
    return arr.map(el => {
        const contacts = [...el.contacts];
        for (let i = contacts.length; i < max; i++) {
            contacts[i] = {contactPerson: null, email: null};
        }
        return {...el, contacts};
    });
}

Or to change in place.

function fillMissingContacts(arr) {
    const max = arr.reduce((max, el) =>
        Math.max(max, el.contacts.length), 0);
    for (const el of arr) {
        for (let i = el.contacts.length; i < max; i++) {
            el.contacts[i] = {contactPerson: null, email: null};
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your code does not seem to do what you described in your question. If you do some modifications to it to look like this:

const array = res?.data?.tools;
// Finding max count of contacts
const max = array ? Math.max(...array.map?.(obj => obj.contacts.length)) : 0;
// Filling master array
const masterArray = (array || []).map(obj => {
  const emptyContact = () => ({ contactPerson: null, email: null });
  // creating contact array; max - contacts.length is the lengths of missing contacts; emptyContact will be called that many times
  const contacts = [ ...obj.contacts, ...Array.from({ length: max - obj.contacts.length }, emptyContact)];
  // Creating new object with new contacts, so we do not overwrite the original
  return { ...obj, contacts };
});

Comments

0

var s = [
  {
    "toolName": "Alteryx",
    "contacts": [
      {
        "contactPerson": "James clear",
        "email": "[email protected]"
      },
      {
        "contactPerson": "Paul Unger",
        "email": "[email protected]"
      }
    ]
  },
  {
    "toolName": "Processes",
    "contacts": [
      {
        "contactPerson": "naomi Unger",
        "email": "[email protected]"
      },
    {
        "contactPerson": null,
        "email": null
      }
    ]
  },
  {
    "toolName": "Alteryx Server",
    "contacts": [
      {
        "contactPerson": "Avinash",
        "email": "[email protected]"
      },
      {
        "contactPerson": "Sowmia",
        "email": "[email protected]"
      }
    ]
  }
]
const handleNullContacts = (obj) => {
obj.map(e=>e?.contacts.map(contact=>{
  if (!contact?.contactPerson)
    contact["contactPerson"] = '';
  if (!contact?.email)
    contact["email"] = ''
  }))
  return obj;
}

s = handleNullContacts(s)
console.log(s)

I'm using map to iterate through the array then another map to iterate through the contacts whenever i find contacts field undefined||null replace with empty string ''
PS: if I missundertood please let me know so I can update the answer

1 Comment

Thanks for answering. Code Block is original code. I want as expectation. If any object is not of same length. then fill that object with null values

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.