如何通过数组合并映射每个结果都相同[lodash]

时间:2019-02-07 17:55:41

标签: javascript arrays lodash

我正在尝试将课程与用户进度数据合并。我相信我有一个指针问题。

我成功地完成了两个数组的内部合并。问题在于循环遍历用户而没有获得附带进度数据的正确课程数据。

课程数据

by

所有用户的进度

by

在用户中循环并合并课程中的进度

   let lessons = [
     {“id”: “0106c568-70c0-4e56-8139-8e7f7d124f95",},
     {“id”: “033e18a2-d470-4fd7-8bdc-53e610f3f784",},
     {“id”: “d60f751c-d7d2-4dc6-9eda-a03bc5ebddc6",},
   ];

const usersProgresses = [ [ { “id”: “cjrtmj9d601b908559oxe8hwk”, “lesson”: “0106c568-70c0-4e56-8139-8e7f7d124f95", “score”: null, }, { “id”: “cjrtmk2hv01bx0855yof2ehj4”, “lesson”: “033e18a2-d470-4fd7-8bdc-53e610f3f784”, “score”: 100, }, { “id”: “cjrtmlohd01cp0855jnzladye”, “lesson”: “3724d7df-311c-46d9-934f-a9c44d9335ae”, “score”: 20, } ], ... ]; 中的预期数据:

   // for each user
   const result = usersProgresses.map(user => {
     // merge progress and lesson data by lesson.id
     const mergedProgress = [...lessons].map(lesson => {
       return _.merge(lesson,_ .find(userProgress, { lesson: lesson.id }));
     });
     return mergedProgress;
   });

但得到:

result

3 个答案:

答案 0 :(得分:5)

您可以在香草js中使用嵌套的.map做这样的事情:

const lessons = [{"id":"0106c568-70c0-4e56-8139-8e7f7d124f95",},{"id":"033e18a2-d470-4fd7-8bdc-53e610f3f784",},{"id":"d60f751c-d7d2-4dc6-9eda-a03bc5ebddc6",}]

const usersProgress = [[{"id":"cjrtmj9d601b908559oxe8hwk","lesson":"0106c568-70c0-4e56-8139-8e7f7d124f95","score":null,},{"id":"cjrtmk2hv01bx0855yof2ehj4","lesson":"033e18a2-d470-4fd7-8bdc-53e610f3f784","score":100,},{"id":"cjrtmlohd01cp0855jnzladye","lesson":"3724d7df-311c-46d9-934f-a9c44d9335ae","score":20,}]]

const output = usersProgress.map(user => lessons.map(lesson =>
    ({...user.find(p => p.lesson == lesson.id), ...lesson }))
 );
  
console.log(output)

答案 1 :(得分:0)

        const usersProgresses = [
            [
            {
                "id": "cjrtmj9d601b908559oxe8hwk",
                "lesson": "0106c568-70c0-4e56-8139-8e7f7d124f95",
                "score": null,
            },
            {
                "id": "cjrtmk2hv01bx0855yof2ehj4",
                "lesson": "033e18a2-d470-4fd7-8bdc-53e610f3f784",
                "score": 100,
            },
            {
                "id": "cjrtmlohd01cp0855jnzladye",
                "lesson": "3724d7df-311c-46d9-934f-a9c44d9335ae",
                "score": 20,
            }
            ],
        ];

        let lessons = [
            {"id": "0106c568-70c0-4e56-8139-8e7f7d124f95",},
            {"id": "033e18a2-d470-4fd7-8bdc-53e610f3f784",},
            {"id": "d60f751c-d7d2-4dc6-9eda-a03bc5ebddc6",},
        ];


        const mergedProgress = usersProgresses.map((Progresses) => {
            return [...lessons].map(lesson => {
                return Object.assign(lesson, Progresses.find((i) => i.lesson === lesson.id));
            });
        })

        console.log(mergedProgress)
    



  

答案 2 :(得分:0)

您可以使用_.keyBy()通过课程ID创建用户课程的字典。创建结果对象时,请使用方括号表示法通过id获得课程,并将其传播({undefined值将被忽略)。

const lessons = [{"id":"0106c568-70c0-4e56-8139-8e7f7d124f95",},{"id":"033e18a2-d470-4fd7-8bdc-53e610f3f784",},{"id":"d60f751c-d7d2-4dc6-9eda-a03bc5ebddc6",}]

const usersProgress = [[{"id":"cjrtmj9d601b908559oxe8hwk","lesson":"0106c568-70c0-4e56-8139-8e7f7d124f95","score":null,},{"id":"cjrtmk2hv01bx0855yof2ehj4","lesson":"033e18a2-d470-4fd7-8bdc-53e610f3f784","score":100,},{"id":"cjrtmlohd01cp0855jnzladye","lesson":"3724d7df-311c-46d9-934f-a9c44d9335ae","score":20,}]]

const result = usersProgress.map(user => {
  const lessonsById = _.keyBy(user, 'lesson') // create a dictionary of lesson by id

  return lessons.map(({ id }) => ({
    ...lessonsById[id], // get the lesson or undefined if not found, and spread
    id
  }))
});
   
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>