【问题标题】:Node.js, map through array of objects, and put specific values into a new arrayNode.js,通过对象数组进行映射,并将特定值放入新数组
【发布时间】:2020-04-25 01:54:32
【问题描述】:

我正在尝试遍历每个对象的评级数组,提取每个内部对象的分数并将它们添加到一个新数组中。

例如,post_id 为“5e1223c2383ce049d8b32eb5”的帖子,我需要一个包含 [1, 4] 的关联数组。和 带有 post_id" "5e146b993dde720850c11c0e" 的帖子将包含 [5] 等。

我们将不胜感激!

[
    {
        "post_id": "5e1223c2383ce049d8b32eb5",
        "ratings": [
            {
                "_id": "5e134aa9d6c3a51930452d49",
                "user": "5e0f76c96a55d6352879daab",
                "score": 1
            },
            {
                "_id": "5e134c00cab61f408c75d2f2",
                "user": "5e0f34adab4d4b369c57fbd4",
                "score": 4
            }
        ]
    },
    {
        "post_id": "5e13592c62008b4e3435472f",
        "ratings": []
    },
    {
        "post_id": "5e146b993dde720850c11c0e",
        "ratings": [
            {
                "_id": "5e1473583dde720850c11c13",
                "user": "5e0f34adab4d4b369c57fbd4",
                "score": 5
            }
        ]
    }
]

【问题讨论】:

  • 您的预期输出是什么? {"5e1223c2383ce049d8b32eb5" : [1, 4]}?

标签: javascript arrays node.js sorting


【解决方案1】:

    const original_array = [
        {
            "post_id": "5e1223c2383ce049d8b32eb5",
            "ratings": [
                {
                    "_id": "5e134aa9d6c3a51930452d49",
                    "user": "5e0f76c96a55d6352879daab",
                    "score": 1
                },
                {
                    "_id": "5e134c00cab61f408c75d2f2",
                    "user": "5e0f34adab4d4b369c57fbd4",
                    "score": 4
                }
            ]
        },
        {
            "post_id": "5e13592c62008b4e3435472f",
            "ratings": []
        },
        {
            "post_id": "5e146b993dde720850c11c0e",
            "ratings": [
                {
                    "_id": "5e1473583dde720850c11c13",
                    "user": "5e0f34adab4d4b369c57fbd4",
                    "score": 5
                }
            ]
        }
    ];
    
    const new_array = original_array.map(item => ({
        post_id: item.post_id,
        ratings: item.ratings.map(item => item.score)
    }));
    
    console.log(new_array);

输出:

[
    {
        post_id: '5e1223c2383ce049d8b32eb5',
        ratings: [1, 4]
    },
    {
        post_id: '5e13592c62008b4e3435472f',
        ratings: []
    },
    {
        post_id: '5e146b993dde720850c11c0e',
        ratings: [5]
    }
]

【讨论】:

  • 最后一行的“=>”有什么作用?这是什么意思?
  • @MoltasDev,这是一个arrow function
  • @MoltasDev 它基本上是function(item) { return item.score; } 的简写方式。 ES6 语法。或者也可以写成item => { return item.score; }等等。
  • @decho ...但不完全相同
  • @Ivan 和 VLAZ 我知道这一点,但您不可能以简短答案的形式涵盖所有可能的细节。所以是的,箭头函数不是常规函数的复本,但在问题的上下文中,它们具有相同的目的。
【解决方案2】:

您可以遍历帖子数组并将每个用户数组映射到分数数组:

data.forEach(post => {
  post.ratings = post.ratings.map(({score}) => score)
});

console.log(data)
<script>const data=[{post_id:"5e1223c2383ce049d8b32eb5",ratings:[{_id:"5e134aa9d6c3a51930452d49",user:"5e0f76c96a55d6352879daab",score:1},{_id:"5e134c00cab61f408c75d2f2",user:"5e0f34adab4d4b369c57fbd4",score:4}]},{post_id:"5e13592c62008b4e3435472f",ratings:[]},{post_id:"5e146b993dde720850c11c0e",ratings:[{_id:"5e1473583dde720850c11c13",user:"5e0f34adab4d4b369c57fbd4",score:5}]}];</script>

您还可以映射整个数组以创建一个单独的对象:

const result = data.map(({ post_id, ratings }) => {
  return { post_id, ratings: ratings.map(({ score }) => score) }
});

console.log(result)
<script>const data=[{post_id:"5e1223c2383ce049d8b32eb5",ratings:[{_id:"5e134aa9d6c3a51930452d49",user:"5e0f76c96a55d6352879daab",score:1},{_id:"5e134c00cab61f408c75d2f2",user:"5e0f34adab4d4b369c57fbd4",score:4}]},{post_id:"5e13592c62008b4e3435472f",ratings:[]},{post_id:"5e146b993dde720850c11c0e",ratings:[{_id:"5e1473583dde720850c11c13",user:"5e0f34adab4d4b369c57fbd4",score:5}]}];</script>

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 2021-04-20
    • 2022-07-25
    • 2019-11-21
    • 2023-01-27
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多