1

Ok, I've been racking my brain and trying this in a variety of methods, but I keep running into deadends. I even tried using Angular to do nested ng-repeats to filter down to the properties I needed but that was too heavy on resources.

What I'm trying to do is look up the value of "indices" for each object in the "groups" array, and find the corresponding object in the "person" array by matching the index to the 'indices' value, and then pushing that value to the corresponding object in the 'groups' array.

I'm trying to do this only in JavaScript when a response is returned from an api.

Example below:

    var obj = {
    "groups": [
        {
          "name": "GroupA",
          "indices": [
            0
          ]
        },
        {
          "name": "GroupB",
          "indices": [
            1
          ]
        },
        {
          "name": "GroupC",
          "indices": [
            2,
            3
          ]
        }
      ],
    "person": [
        {"name": "Archer"},
        {"name": "Lana"},
        {"name": "Mother"},
        {"name": "Barry"}
      ]
  };

And this is what I want the final object to look like:

 var obj = {
    "groups": [
        {
          "name": "GroupA",
          "indices": [
            0
          ],
        "person": [
          {"name": "Archer"}
          ]
        },
        {
          "name": "GroupB",
          "indices": [
            1
          ],
          "person": [
            {"name": "Lana"}
          ]
        },
        {
          "name": "GroupC",
          "indices": [
            2,
            3
          ],
          "person": [
            {"name": "Mother"},
            {"name": "Barry"}
          ]
        }
      ]
  };

1 Answer 1

2

I would use forEach for the outer loop rather than map but it's a matter of style more than substance:

 obj.groups.forEach(g => g.person = g.indices.map(i => obj.person[i]))

var obj = {
    "groups": [
        {
          "name": "GroupA",
          "indices": [0]
        },
        {
          "name": "GroupB",
          "indices": [ 1 ]
        },
        {
          "name": "GroupC",
          "indices": [2,3 ]
        }
      ],
    "person": [
        {"name": "Archer"},
        {"name": "Lana"},
        {"name": "Mother"},
        {"name": "Barry"}
      ]
  };

  obj.groups.forEach(g =>  g.person = g.indices.map(i => obj.person[i]))
  console.log(obj.groups)

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

2 Comments

This mutates the input tho. Could or could not be a problem but with map it's never a problem.
@JohanP Yes, it does. That's how I read the question: 'and then pushing that value to the corresponding object in the 'groups' array.'

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.