0

I'm trying to go through the ratings array for each object, extract the scores for each inner object and add them into a new array.

For example, the post with post_id: "5e1223c2383ce049d8b32eb5", I need an associated array which would containe [1, 4]. And the post with post_id" "5e146b993dde720850c11c0e" would contain [5] etc.

Help would be much appreciated!

[
    {
        "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
            }
        ]
    }
]

CodingNoob
  • 13
  • 2

2 Answers2

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

Output:

[
    {
        post_id: '5e1223c2383ce049d8b32eb5',
        ratings: [1, 4]
    },
    {
        post_id: '5e13592c62008b4e3435472f',
        ratings: []
    },
    {
        post_id: '5e146b993dde720850c11c0e',
        ratings: [5]
    }
]
decho
  • 785
  • 6
  • 20
  • What does the "=>" do on the last row? What does it mean? – MoltasDev Jan 07 '20 at 14:44
  • @MoltasDev, it's an [arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) – Ivan Jan 07 '20 at 14:48
  • @MoltasDev it's basically a short way of saying `function(item) { return item.score; }`. ES6 syntax. Alternatively it could also be written as `item => { return item.score; }` and so on. – decho Jan 07 '20 at 14:48
  • @decho ...but not quite the same – Ivan Jan 07 '20 at 14:48
  • Clarify what do you mean @Ivan? – decho Jan 07 '20 at 14:50
  • @decho [Are 'Arrow Functions' and 'Functions' equivalent / exchangeable?](https://stackoverflow.com/questions/34361379/are-arrow-functions-and-functions-equivalent-exchangeable) – VLAZ Jan 07 '20 at 14:52
  • @decho, most notably, arrow functions do not have their own `this`. They are bound to the `this` of the enclosing scope ([you can read more here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions#No_separate_this)) – Ivan Jan 07 '20 at 14:52
  • 1
    @Ivan and VLAZ I am aware of this, but you can't possibly cover every possible detail in the form of a short answer. So yes arrow functions are not a carbon copy of a regular function, but in the context of the question, they serve the same purpose. – decho Jan 07 '20 at 14:56
0

You can loop through your array of posts and map each array of users to an array of scores:

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>

You could also map the whole array to create a seperate object:

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>
Ivan
  • 34,531
  • 8
  • 55
  • 100