0

I have 2 array of objects that look like this

var pollAnswers = [
        {
            "_id": "5b58afa0c767e12c9869e540",
            "pollId": "5b58afa0c767e12c9869e53f",
            "option": "Google",
        },
        {
            "_id": "5b58afa0c767e12c9869e541",
            "pollId": "5b58afa0c767e12c9869e53f",
            "option": "The Jetsons",
        },
        {
            "_id": "5b58afa0c767e12c9869e542",
            "pollId": "5b58afa0c767e12c9869e53f",
            "option": "Family  Guy",
        },
        {
            "_id": "5b593b195c420e28089daf9d",
            "pollId": "5b593b195c420e28089daf9c",
            "option": "Yes. Through loyalty programmes.",
        },
        {
            "_id": "5b593b195c420e28089daf9e",
            "pollId": "5b593b195c420e28089daf9c",
            "option": "What Hunger Crisis?",
        },
        {
            "_id": "5b5953d775c4401e7052127c",
            "pollId": "5b5953d775c4401e7052127b",
            "option": "Yes, absolutely",
        },
        {
            "_id": "5b5953d775c4401e7052127d",
            "pollId": "5b5953d775c4401e7052127b",
            "option": "No, absolutely not",
        }
    ]

var polls = [
        {
            "_id": "5b58afa0c767e12c9869e53f",
            "pollName": "Consumers in 2070 (How about now?)",
            "pollQuestion": "Which animated series will consumers in 2070 resemble the most?",
        },
        {
            "_id": "5b593b195c420e28089daf9c",
            "pollName": "World Hunger",
            "pollQuestion": "Can Internet-based services solve the Hunger Crisis?",
        },
        {
            "_id": "5b5953d775c4401e7052127b",
            "pollName": "Make things Work Again",
            "pollQuestion": "Make things Work",
        }
    ]

I need to compare the pollId from pollAnsers with _id in polls to add the matching answers to the corresponding pollQuestions in the following way

"polls": [
        {
            "_id": "5b58afa0c767e12c9869e53f",
            "pollName": "Consumers in 2070 (How about now?)",
            "pollQuestion": "Which animated series will consumers in 2070 resemble the most?",
            "answersList": [
              {
                "_id": "5b58afa0c767e12c9869e540",
                "pollId": "5b58afa0c767e12c9869e53f",
                "option": "Google",
              },
              {
                "_id": "5b58afa0c767e12c9869e541",
                "pollId": "5b58afa0c767e12c9869e53f",
                "option": "The Jetsons",
              },
              {
                "_id": "5b58afa0c767e12c9869e542",
                "pollId": "5b58afa0c767e12c9869e53f",
                "option": "Family  Guy",
              },             
            ]
        },
        {
            "_id": "5b593b195c420e28089daf9c",
            "pollName": "World Hunger",
            "pollQuestion": "Can Internet-based services solve the Hunger Crisis?",
            "answersList": [
                {
                  "_id": "5b593b195c420e28089daf9d",
                  "pollId": "5b593b195c420e28089daf9c",
                  "option": "Yes. Through loyalty programmes.",
                },
                {
                  "_id": "5b593b195c420e28089daf9e",
                  "pollId": "5b593b195c420e28089daf9c",
                  "option": "What Hunger Crisis?",
                }
            ]
        },
        {
            "_id": "5b5953d775c4401e7052127b",
            "pollName": "Make things Work Again",
            "pollQuestion": "Make things Work",
            "answersList": [
              {
                "_id": "5b5953d775c4401e7052127c",
                "pollId": "5b5953d775c4401e7052127b",
                "option": "Yes, absolutely",
              },
              {
                "_id": "5b5953d775c4401e7052127d",
                "pollId": "5b5953d775c4401e7052127b",
                "option": "No, absolutely not",
              }
            ]
        }
    ]

I have been trying all possibilities like using map, filter, for loops etc but haven't been able to get the result, I am fairly new to this please help ! thanks

2
  • please add your try as well. Commented Jul 27, 2018 at 8:41
  • @mplungjan I did search for literally 2 hours but since I am newbie to javascript I was not able to get the right way to apply things. Commented Jul 27, 2018 at 9:24

5 Answers 5

1

You can use two forEach() loops with simple and understandable code:

var pollAnswers = [
        {
            "_id": "5b58afa0c767e12c9869e540",
            "pollId": "5b58afa0c767e12c9869e53f",
            "option": "Google",
        },
        {
            "_id": "5b58afa0c767e12c9869e541",
            "pollId": "5b58afa0c767e12c9869e53f",
            "option": "The Jetsons",
        },
        {
            "_id": "5b58afa0c767e12c9869e542",
            "pollId": "5b58afa0c767e12c9869e53f",
            "option": "Family  Guy",
        },
        {
            "_id": "5b593b195c420e28089daf9d",
            "pollId": "5b593b195c420e28089daf9c",
            "option": "Yes. Through loyalty programmes.",
        },
        {
            "_id": "5b593b195c420e28089daf9e",
            "pollId": "5b593b195c420e28089daf9c",
            "option": "What Hunger Crisis?",
        },
        {
            "_id": "5b5953d775c4401e7052127c",
            "pollId": "5b5953d775c4401e7052127b",
            "option": "Yes, absolutely",
        },
        {
            "_id": "5b5953d775c4401e7052127d",
            "pollId": "5b5953d775c4401e7052127b",
            "option": "No, absolutely not",
        }
    ]

var polls = [
        {
            "_id": "5b58afa0c767e12c9869e53f",
            "pollName": "Consumers in 2070 (How about now?)",
            "pollQuestion": "Which animated series will consumers in 2070 resemble the most?",
        },
        {
            "_id": "5b593b195c420e28089daf9c",
            "pollName": "World Hunger",
            "pollQuestion": "Can Internet-based services solve the Hunger Crisis?",
        },
        {
            "_id": "5b5953d775c4401e7052127b",
            "pollName": "Make things Work Again",
            "pollQuestion": "Make things Work",
        }
];

polls.forEach((poll) => {
  poll.answerList = [];
  pollAnswers.forEach((pollAnswer) => {
    if(pollAnswer.pollId === poll._id){
      poll.answerList.push(pollAnswer);
    }
  });
});
console.log(polls);

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

1 Comment

@hushie glad to help
1

You can loop over the polls using Array#forEach, in each iteration, find the related answers using Array#filter, and assign them to the poll object:

var pollAnswers = [{"_id":"5b58afa0c767e12c9869e540","pollId":"5b58afa0c767e12c9869e53f","option":"Google"},{"_id":"5b58afa0c767e12c9869e541","pollId":"5b58afa0c767e12c9869e53f","option":"The Jetsons"},{"_id":"5b58afa0c767e12c9869e542","pollId":"5b58afa0c767e12c9869e53f","option":"Family  Guy"},{"_id":"5b593b195c420e28089daf9d","pollId":"5b593b195c420e28089daf9c","option":"Yes. Through loyalty programmes."},{"_id":"5b593b195c420e28089daf9e","pollId":"5b593b195c420e28089daf9c","option":"What Hunger Crisis?"},{"_id":"5b5953d775c4401e7052127c","pollId":"5b5953d775c4401e7052127b","option":"Yes, absolutely"},{"_id":"5b5953d775c4401e7052127d","pollId":"5b5953d775c4401e7052127b","option":"No, absolutely not"}];
var polls = [{"_id":"5b58afa0c767e12c9869e53f","pollName":"Consumers in 2070 (How about now?)","pollQuestion":"Which animated series will consumers in 2070 resemble the most?"},{"_id":"5b593b195c420e28089daf9c","pollName":"World Hunger","pollQuestion":"Can Internet-based services solve the Hunger Crisis?"},{"_id":"5b5953d775c4401e7052127b","pollName":"Make things Work Again","pollQuestion":"Make things Work"}];

polls.forEach(poll => {
  poll.answersList = pollAnswers.filter(ans => ans.pollId === poll._id);
})

console.log(polls);

Comments

1

You could take the power of Map and iterate the target array first to fill the map and generate independent objects for the result set.

Then iterate all answers and assign them to the associated polls.

var pollAnswers = [{ _id: "5b58afa0c767e12c9869e540", pollId: "5b58afa0c767e12c9869e53f", option: "Google" }, { _id: "5b58afa0c767e12c9869e541", pollId: "5b58afa0c767e12c9869e53f", option: "The Jetsons" }, { _id: "5b58afa0c767e12c9869e542", pollId: "5b58afa0c767e12c9869e53f", option: "Family  Guy" }, { _id: "5b593b195c420e28089daf9d", pollId: "5b593b195c420e28089daf9c", option: "Yes. Through loyalty programmes." }, { _id: "5b593b195c420e28089daf9e", pollId: "5b593b195c420e28089daf9c", option: "What Hunger Crisis?" }, { _id: "5b5953d775c4401e7052127c", pollId: "5b5953d775c4401e7052127b", option: "Yes, absolutely" }, { _id: "5b5953d775c4401e7052127d", pollId: "5b5953d775c4401e7052127b", option: "No, absolutely not" }],
    polls = [{ _id: "5b58afa0c767e12c9869e53f", pollName: "Consumers in 2070 (How about now?)", pollQuestion: "Which animated series will consumers in 2070 resemble the most?" }, { _id: "5b593b195c420e28089daf9c", pollName: "World Hunger", pollQuestion: "Can Internet-based services solve the Hunger Crisis?" }, { _id: "5b5953d775c4401e7052127b", pollName: "Make things Work Again", pollQuestion: "Make things Work" }],
    map = new Map,
    result = {
        polls: polls.map(p => {
            var answersList = [];
            map.set(p._id, answersList);
            return Object.assign({}, p, { answersList });
        })
    };

pollAnswers.forEach(p => map.get(p.pollId).push(p));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

I now get what mistake I was making when I tried to use map ! thanks
1

My solution with reduce :

   var pollsWithAnswers = pollAnswers.reduce(function(polls, answer) {
      var index = polls.findIndex(function(pol) {
        return pol._id == answer.pollId;
      })
      if (index > -1) {
        if (polls[index].anwerList instanceof Array) polls[index].anwerList.push(answer);
        else polls[index].anwerList = [answer]
      }
      return polls
    }, polls.slice())

var pollAnswers = [{
    "_id": "5b58afa0c767e12c9869e540",
    "pollId": "5b58afa0c767e12c9869e53f",
    "option": "Google",
  },
  {
    "_id": "5b58afa0c767e12c9869e541",
    "pollId": "5b58afa0c767e12c9869e53f",
    "option": "The Jetsons",
  },
  {
    "_id": "5b58afa0c767e12c9869e542",
    "pollId": "5b58afa0c767e12c9869e53f",
    "option": "Family  Guy",
  },
  {
    "_id": "5b593b195c420e28089daf9d",
    "pollId": "5b593b195c420e28089daf9c",
    "option": "Yes. Through loyalty programmes.",
  },
  {
    "_id": "5b593b195c420e28089daf9e",
    "pollId": "5b593b195c420e28089daf9c",
    "option": "What Hunger Crisis?",
  },
  {
    "_id": "5b5953d775c4401e7052127c",
    "pollId": "5b5953d775c4401e7052127b",
    "option": "Yes, absolutely",
  },
  {
    "_id": "5b5953d775c4401e7052127d",
    "pollId": "5b5953d775c4401e7052127b",
    "option": "No, absolutely not",
  }
]

var polls = [{
    "_id": "5b58afa0c767e12c9869e53f",
    "pollName": "Consumers in 2070 (How about now?)",
    "pollQuestion": "Which animated series will consumers in 2070 resemble the most?",
  },
  {
    "_id": "5b593b195c420e28089daf9c",
    "pollName": "World Hunger",
    "pollQuestion": "Can Internet-based services solve the Hunger Crisis?",
  },
  {
    "_id": "5b5953d775c4401e7052127b",
    "pollName": "Make things Work Again",
    "pollQuestion": "Make things Work",
  }
]


console.log(pollAnswers.reduce(function(polls, answer) {
  var index = polls.findIndex(function(pol) {
    return pol._id == answer.pollId;
  })
  if (index > -1) {
    if (polls[index].anwerList instanceof Array) polls[index].anwerList.push(answer);
    else polls[index].anwerList = [answer]
  }
  return polls
}, polls.slice()))

1 Comment

Thanks this works too ! I tried reduce but did not know where I was going wrong
0

This is another solution to use map and filter with simple and immutable way so your original arrays will not be mutated.

var pollAnswers = [{"_id":"5b58afa0c767e12c9869e540","pollId":"5b58afa0c767e12c9869e53f","option":"Google"},{"_id":"5b58afa0c767e12c9869e541","pollId":"5b58afa0c767e12c9869e53f","option":"The Jetsons"},{"_id":"5b58afa0c767e12c9869e542","pollId":"5b58afa0c767e12c9869e53f","option":"Family  Guy"},{"_id":"5b593b195c420e28089daf9d","pollId":"5b593b195c420e28089daf9c","option":"Yes. Through loyalty programmes."},{"_id":"5b593b195c420e28089daf9e","pollId":"5b593b195c420e28089daf9c","option":"What Hunger Crisis?"},{"_id":"5b5953d775c4401e7052127c","pollId":"5b5953d775c4401e7052127b","option":"Yes, absolutely"},{"_id":"5b5953d775c4401e7052127d","pollId":"5b5953d775c4401e7052127b","option":"No, absolutely not"}];
var polls = [{"_id":"5b58afa0c767e12c9869e53f","pollName":"Consumers in 2070 (How about now?)","pollQuestion":"Which animated series will consumers in 2070 resemble the most?"},{"_id":"5b593b195c420e28089daf9c","pollName":"World Hunger","pollQuestion":"Can Internet-based services solve the Hunger Crisis?"},{"_id":"5b5953d775c4401e7052127b","pollName":"Make things Work Again","pollQuestion":"Make things Work"}];

const pollsWithAnswers = polls.map(poll => ({
   ...poll,
   answersList: pollAnswers.filter(answer => poll._id === answer.pollId) 
}));

console.log(pollsWithAnswers);

Hope it helps

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.