0

I have to create an array of arrays based on some object attributes.

So my object looks like this:

const data = {
 projectId: 5,
 userIds: [2, 3, 1, 5],
 dateIds: [99, 100, 101, 102, 103], 
 task: 'task', 
 duration: 8, 
 description: 'description'
}

Based on the userIds and dateIds I have to create an array of arrays with every attribute like this:

[[projectId, userId, dateId, task, duration, description]] <- this is what every number means

For every userId and dateId i have to create a new array.

And based on my example should be like this:

[[5, 2, 99, 'task', 8, 'description'], 
[5, 3, 99 , 'task', 8, 'description'], 
[5, 1, 99, 'task', 8, 'description'], 
[5, 5, 99, 'task', 8, 'description'], 
[5, 2, 100, 'task', 8, 'description'], 
[5, 3, 100, 'task', 8, 'description'] 
... etc]]

Hope i explained my issue well. Thank you for your time!

My function:

const data = { projectId: 5, userIds: [2, 3, 1, 5], date: [99, 100, 101, 102], task: 'task', duration: 'duration', description: 'description' }
    const parentArray = []
        data.date.map(object =>
            data.userIds.map(anotherObject => {
                // console.log(anotherObject, object)
                parentArray.push([data.projectId, object, anotherObject, data.task, data.duration, data.description])
            }
            ))
    console.log(parentArray)

6
  • 1
    Have you tried anything? Commented May 24, 2021 at 11:04
  • I tried to map first by userId and then by dateId and somehow push them into a new array but it didn't worked Commented May 24, 2021 at 11:04
  • Are userId and dateId always guaranteed to be of the same length? Commented May 24, 2021 at 11:06
  • @painotpi no, they can have any length from 1 to n, but can never be empty Commented May 24, 2021 at 11:07
  • 1
    @poPaTheGuru please add the code you tried. Commented May 24, 2021 at 11:07

2 Answers 2

2

const data = {
 projectId: 5,
 userIds: [2, 3, 1, 5],
 dateIds: [99, 100, 101, 102, 103], 
 task: 'task', 
 duration: 8, 
 description: 'description'
}

const result = data.userIds.map(uid => {
  return data.dateIds.map(did => [data.projectId, uid, did, data.task, data.duration, data.description])
}).flat();

console.log(result);

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

Comments

1

Not exactly what you asked for but using your example you can create an array of objects which might add clarity by naming the properties (if some other user needs something like this). Note how I pass in the data and reference it with this and loop through the dateId's. Key is I never have to reference the original array, perhaps making this more maintainable internally;

const data = {
  projectId: 5,
  userIds: [2, 3, 1, 5],
  dateIds: [99, 100, 101, 102, 103],
  task: 'task',
  duration: 8,
  description: 'description'
};
let x = [];
data.userIds.forEach(function(userid, index) {
  this.dateIds.map((dateid, idx) => {
    x.push({
      project: this.projectId,
      user: userid,
      dateid: dateid,
      tsk: this.task,
      dur: this.duration,
      desc: this.description
    });
  });
}, data);

x.forEach(function(el, index, array) {
  console.log(el);
});

1 Comment

Nice intervention. I needed just the array, without any description that's why I didn't added them :D

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.