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

1
  • 1
    What is your expected output? {"5e1223c2383ce049d8b32eb5" : [1, 4]}? Commented Jan 7, 2020 at 14:34

2 Answers 2

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]
    }
]
Sign up to request clarification or add additional context in comments.

8 Comments

What does the "=>" do on the last row? What does it mean?
@MoltasDev, it's an arrow function
@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 ...but not quite the same
@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.
|
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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.