0

i want under admin should have test#1 & test#4, under manager should have test#2 and under head should have test#3. I have tried but couldnt do it as per requirement

link : https://jsbin.com/cecafeqobo/edit?js,console below is my code:

var mapData = [];

var userList =[{id:2,name:'Admin', users:[{id:2, name:'Test#1'},{id:3,name:'test#4'}]},{id:2,name:'Manager', users:[{id:2, name:'test#2'}]},{id:2,name:'Head', users:[{id:2, name:'test#3'}]}];
userList.forEach(function(element) {
  console.log(element.name)
  element.users.forEach(function (element) {
       mapData.push(element);
  })
})

mapData.forEach((element) => {
  console.log(element.name);
});

0

2 Answers 2

1

Have you tried using some if/elses? E.g.

admin_users = [];
manager_users = [];

userList.forEach(function (root_element) {
  element.users.forEach(function (element) {
    switch (root_element.name) {
      case 'Admin':
        admin_users.push(element);
        break;
      // Same for other types of users.
    }
  })
})

Your admin users will then be saved in the admin_users variable, and your manager users in the manager_users variable.

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

3 Comments

case 'Admin': is dynamic i can get Admin Head Admin senior etc its up to moderator
Then perhaps have an object with dynamic keys and the elements under those dynamic keys?
solved thanks anyway i just used @Adriani6 with when first push done that time actually i am sending dom element <b> Admin </b> on dom bind to [innerHtml]
0

That's because you're logging all the users as a one in the last forEach. Which is giving you this output. First you output all the 'headings' then you output all the users.

Rather than appending users to an array, you should consider logging it.

userList.forEach(function(element) {
  console.log(element.name)
  element.users.forEach(function (element) {
       console.log(element.name);
  })
})

var userList =[{id:2,name:'Admin', users:[{id:2, name:'Test#1'},{id:3,name:'test#4'}]},{id:2,name:'Manager', users:[{id:2, name:'test#2'}]},{id:2,name:'Head', users:[{id:2, name:'test#3'}]}];

userList.forEach(function(element) {
  console.log(element.name)
  element.users.forEach(function (element) {
       console.log(element.name);
  })
})

6 Comments

Indeed your way is worked but how can i render Admin, Manager, Head to be bold and style which is dynamic
@Indraraj26 This was not part of your question. You have two console.logs now, the first one is your titles, apply styles to that.
i did but there is no way to find which one title and name because in my array and array of object has same key
@Indraraj26 Try a switch statement or an if/else.
array name i.e title is dynamic like if(name== 'Admin') make bold that i cant do i have to find another way
|

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.