I have a "main" function that reads data from input data and generates an output dtoOut of a kind of Employee ID with a random name, surname, etc.
Example of input data
const dtoIn = {
count: 50,
age: {
min: 19,
max: 35
}
}
Example of output data
const dtoOut = [
{
gender: "male",
birthdate: "1993-08-07T00:00:00.000Z",
name: "Vratislav",
surname: "Sýkora",
workload: 40
}
with this code:
// main function
function main(input_data) {
// how many employees work at the firm
let dtoIn_counter = input_data.count
// minimal age of employees
let dtoIn_min = input_data.age.min
// maximal age of employees
let dtoIn_max = input_data.age.max
//output data
const dtoOut = {}
// for loop that counts the number of employees and repeats the process for each one
for (let i = 0; i < dtoIn_counter; i ) {
// randomly decides if the employee will be male or female and assigns first and last name
const male_female = getRandomArbitrary(1, 3)
if (male_female === 1){
dtoOut.name = male_name1[getRandomArbitrary(0, male_name1.length)]
dtoOut.surname = male_name2[getRandomArbitrary(0, male_name2.length)]
dtoOut.gender = "male"
dtoOut.workload = workload[getRandomArbitrary(0, workload.length)]
}
else {
dtoOut.name = female_name1[getRandomArbitrary(0, female_name1.length)]
dtoOut.surname = female_name2[getRandomArbitrary(0, female_name2.length)]
dtoOut.gender = "female"
dtoOut.workload = workload[getRandomArbitrary(0, workload.length)]
}
}
return dtoOut
}
I am struggling with how to put more of these "dictionaries" in the const without overwriting the already-made dictionary. So from my understanding is that what this code does so far is overwrite the output code 50 times and I would like to fix it but not really sure how.
Also if anybody knew how to randomly assign a birthrate from min, max age object and put it into the ISO Date-Time format - YYYY-MM-DDTHH:MM:SSZ that would be awesome.
Hopefully this question wont be too broad and I was clear in what I need help with. Thanks!
CodePudding user response:
Are you looking to get back something like:
const result = main(input_data)
// Where result is:
[
{
gender: "male",
birthdate: "1993-08-07T00:00:00.000Z",
name: "Vratislav",
surname: "Sýkora",
workload: 40
},
... // <-- a bunch more objects
]
Maybe something like this?
const inputData = {
count: 50,
age: {
min: 18,
max: 50
}
}
function main(input_data) {
let dtoIn_counter = input_data.count
let dtoIn_min = input_data.age.min
let dtoIn_max = input_data.age.max
//output data
// turn this into an array to collect the entries in your for loop
const dtoOut = [] // <-- change to array
for (let i = 0; i < dtoIn_counter; i ) {
// we'll create a new object each time to get the employee data
let employee = {} // <-- use to track employee
// you'll have to reuse your existing logic but the idea is here
employee.name = `some name - ${i}`
employee.surname = `some surname - ${i}`
employee.gender = "male"
employee.workload = `a workload - ${i}`
// add this employee to the dtoOut array
dtoOut.push(employee) // <-- add employee to array
}
return dtoOut // finally, return the entire collection
}
const result = main(inputData)
console.log(result)
CodePudding user response:
If you wanna set random names with no repeat and without changing the original random data arrays, you can store their indexes in arrays and remove the index of the random item that has been selected from that array each time.
const dtoOut = [{
gender: "male",
birthdate: "1993-08-07T00:00:00.000Z",
name: "Vratislav",
surname: "Sýkora",
workload: 40
}]
const input_data = {
count: 4,
age: {
min: 19,
max: 35
}
}
const randWorkloads = [40, 56, 12, 14]
const male_name1 = ["David", "Json", "Coby", "Tyler"]
const male_name2 = ["Davidson", "johnson", "Briant", "Smith"]
const female_name1 = ["Mary", "Rose", "Lilly", "Monica"]
const female_name2 = ["Williams", "Doe", "Joseph", "Hart"]
let workloadIndexes = [...Array(male_name1.length).keys()]
let maleNameIndexes = [...Array(male_name1.length).keys()];
let maleSurNameIndexes = [...Array(male_name2.length).keys()];
let femaleNameIndexes = [...Array(female_name1.length).keys()];
let femaleSurNameIndexes = [...Array(female_name2.length).keys()];
function getRandomArbitrary(min, max) {
return parseInt(Math.floor(Math.random() * (max - min) min));
}
function main(input_data) {
// how many employees work at the firm
let dtoIn_counter = input_data.count
// minimal age of employees
let dtoIn_min = input_data.age.min
// maximal age of employees
let dtoIn_max = input_data.age.max
//output data
const arr = [...Array(dtoIn_counter).keys()]
const result = arr.reduce((acc, item) => {
const gender = getRandomArbitrary(1, 3) === 1 ? "male" : "female";
let name;
let surname;
let workloadIndex = Math.floor(Math.random() * (workloadIndexes.length - 1));
let index = workloadIndexes[workloadIndex];
const workload = randWorkloads[index];
workloadIndexes.splice(workloadIndex, 1);
const age = parseInt(getRandomArbitrary(dtoIn_min, dtoIn_max 1))
if (gender === "female") {
let nameIndex = Math.floor(Math.random() * (femaleNameIndexes.length - 1));
let index1 = femaleNameIndexes[nameIndex];
name = female_name1[index1];
femaleNameIndexes.splice(nameIndex, 1);
let surnameIndex = Math.floor(Math.random() * (femaleSurNameIndexes.length - 1));
let index2 = femaleSurNameIndexes[surnameIndex];
surname = female_name2[index2];
femaleSurNameIndexes.splice(surnameIndex, 1);
} else {
let nameIndex = Math.floor(Math.random() * (maleNameIndexes.length - 1));
let index1 = maleNameIndexes[nameIndex];
name = male_name1[index1];
maleNameIndexes.splice(nameIndex, 1);
let surnameIndex = Math.floor(Math.random() * (maleSurNameIndexes.length - 1));
let index2 = maleSurNameIndexes[surnameIndex];
surname = male_name2[index2];
maleSurNameIndexes.splice(surnameIndex, 1);
}
const newEmployee = {
name,
surname,
gender,
workload,
age
}
return [...acc, newEmployee]
}, [])
return result
}
console.log(main(input_data))