Im trying to get a function put together on a project. I have an array being generated by a function that is made up of objects. The first layer of the array is company, and then inside each company are the users.
First and foremost, I need to parse through all the users and change any instance of the FirstName or LastName where undefined to be an empty string.
I have done this successfully, but the issue is that my code seems to be impacting both the original array (which I do not want to touch at all), and my copy. When I run this code, the undefined names are changed to empty strings in both.
I have tried every way imaginable to copy the array and it does not seem to matter which method I use. JSON.Stringify almost looked like it would work, but it completely eliminiated all undefined properties from the source array, which is a no go.
The issue remains, so I suspect the issue is with my code for the replacement itself, shown below.
const companies = createAll();
const copyCompanies = companies.slice();
const changeUndefined = (companiesAtt) => {
const usersArr = companiesAtt.map((company) => {
return company.users.map((eachUser) => {
for (let property in eachUser) {
if (eachUser[property] === undefined) {
eachUser[property] = '';
}
}
return eachUser;
});
});
return usersArr;
};
cleanConsole(1, companies);
console.log(
'----SOLUTION EXAMPLE 1 --- ',
changeUndefined(copyCompanies)
);
The source data and functions are below:
const firstName = [
'joe',
'jane',
'seb',
'alina',
'fabien',
'merwan',
'anna',
'annah',
'Fathma',
'Mohamed',
'Walid',
'Josiane',
undefined,
];
const lastName = [
'Dupont',
'Dupond',
'Durand',
'Kassir',
'Dalachra',
'Hussein',
'Wartani',
'Thoumsi',
'Angello',
undefined,
];
const companyNames = [
'apple',
'microsoft',
'Louis-vuitton',
'Nike',
'Channel',
'Dior',
'Slack',
'Uber',
];
function createRandomNumber(min, max) {
return Math.floor(min + Math.random() * (max - min));
}
function getValue(array) {
return array[createRandomNumber(0, array.length + 2)];
}
function createCompany(name, index) {
const users = createUser(createRandomNumber(10, 50));
return {
name,
users,
isOpen: !!createRandomNumber(0, 2),
usersLength: users.length,
id: index,
};
}
function createUser(end) {
const tab = [];
for (let i = 0; i < end; i++) {
tab.push({
firstName: getValue(firstName),
lastName: getValue(lastName),
age: createRandomNumber(10, 120),
car: !!createRandomNumber(0, 2),
id: tab.length,
});
}
return tab;
}
let companies;
export function createAll() {
if (!companies) {
companies = companyNames.map(createCompany);
}
return companies;
}
export function clone(value) {
return Object.keys(value).map((key) => {
value[key].users = value[key].users.map((user) => ({ ...user }));
value[key] = { ...value[key] };
return value[key];
});
}
export function cleanConsole(index, value) {
const result = clone(value);
console.log(
`---- EXAMPLE ${index} --- values before your modifications :`,
result
);
}
Any ideas would be greatly appreciated. I feel like I am missing something important.