比较两个对象数组,并将具有匹配值的对象添加到第一个对象数组

时间:2018-07-27 08:37:53

标签: javascript arrays json node.js javascript-objects

我有2个看起来像这样的对象数组

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",
        }
    ]

我需要将pollAnsers中的pollId与民意测验中的_id进行比较,以通过以下方式将匹配的答案添加到相应的pollQuestions中

"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",
              }
            ]
        }
    ]

我一直在尝试所有可能的方法,例如使用map,filter,for循环等,但未能获得结果,对此我还是很陌生,请帮助!谢谢

5 个答案:

答案 0 :(得分:1)

您可以使用Array#forEach遍历民意调查,在每次迭代中,使用Array#filter查找相关答案,并将其分配给民意调查对象:

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);

答案 1 :(得分:1)

您可以使用两个forEach()循环使用简单易懂的代码:

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);

答案 2 :(得分:1)

您可以利用Map的功效并首先迭代目标数组以填充地图并为结果集生成独立的对象。

然后迭代所有答案并将其分配给相关的民意测验。

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; }

答案 3 :(得分:1)

我的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()))

答案 4 :(得分:0)

这是另一种使用mapfilter且简单且不可变的方式的解决方案,这样您的原始数组就不会发生突变。

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);

希望有帮助