0

I have this array :

var hemicycle = {

    Group1 : [{
      GroupName : "Les bests",
        Member1 : [{
            Name : "Loris Plasson",
            Seat : 4,
            Vignette : "PhotoURL"
        }],
        Member2 : [{
          Name : "Anne-Sophie",
          Seat : 3,
          Vignette : "PhotoURL"
      }]

    }]

I want to push the object Member1 or Member2 on another object depending of the Seat value.

To do that I think I need to "search" for the Seat value with a for loop and retrieve the object, but all the examples I found on StackOverflow were with simple arrays like this :

var array = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];

With those simple arrays they are able to use something like a for loop with array[i].

But in my case I really don't know what to do...

UPDATE : What I want : The Member object which include the corresponding Seat value searched. Then I push the Member object to another object.

Thanks for any help.

14
  • What you expect as end result? Commented Oct 26, 2017 at 14:16
  • 1
    but each member is an array as well, for example ` Member1 : [{ Name : "Loris Plasson", Seat : 4, Vignette : "PhotoURL" }],`. Why should it be an array? Commented Oct 26, 2017 at 14:24
  • 1
    This is how your data should look: hemicycle = [ { GroupName : "Les bests", members: [{ Name : "Loris Plasson", Seat : 4, Vignette : "PhotoURL" }, { Name : "Anne-Sophie", Seat : 3, Vignette : "PhotoURL" }] } ] Commented Oct 26, 2017 at 14:42
  • 1
    It's an array of groups. Each group is an object, that has the members property, which is an array of member objects. Commented Oct 26, 2017 at 14:44
  • 1
    You're welcome :) I've added an answer that includes how to find the member. Your solution won't work if the member is in the 2nd group. Commented Oct 26, 2017 at 18:14

1 Answer 1

1

The data structure that you use doesn't reflect what you are trying to convey, and in addition is very heard to traverse.

I suggest creating an array of groups. Each group is an object, that has the members property, which is an array of member objects:

[{
  "GroupName": "Les bests",
  "members": [{
      "Name": "Loris Plasson",
      "Seat": 4,
      "Vignette": "PhotoURL"
    },
    {
      "Name": "Anne-Sophie",
      "Seat": 3,
      "Vignette": "PhotoURL"
    }
  ]
}]

Using this structure, you find a member using 2 for loops - one to iterate the groups, and the other to iterate the members of each group. Once a member is found, the function returns the member's object immediately. If not undefined is returned:

var groups = [{"GroupName":"Les bests","members":[{"Name":"Loris Plasson","Seat":4,"Vignette":"PhotoURL"},{"Name":"Anne-Sophie","Seat":3,"Vignette":"PhotoURL"}]}];

var seatNum = 4;

function findMember(seatNum) {
  var members;
  for(var i = 0; i < groups.length; i++) {
    members = groups[i].members;
    for(var j = 0; j < members.length; j++) {
      if(members[j].Seat = seatNum) {
        return members[j];
      }
    }
  }
}

var member = findMember(seatNum);

console.log(member);

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

Comments

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.