Filter JSON by key value with JavaScript

you have to use lodash to group data, then map data

var data = {
                "key":"userSubscriptions",
                "value":
                [{"channel":"Netflix","user":"Bobby","region":"NA"},
                {"channel":"Netflix","user":"Bobby","region":"EU"},
                {"channel":"Netflix","user":"Jamie","region":"SEA"},
                {"channel":"Prime Video","user":"Bobby","region":"NA"}]
            }

            var users = _.chain(data.value)
                .groupBy("user").map((value, key) => ({ user: key, data: value })).value();

                users.forEach(element => {
                    console.log(element)

                    console.log(`${element.user}, your region subscriptions are: Netflix: ${element.data.map(c=>c.channel).join(',')}, Prime Video: ${element.data.map(c=>c.region).join(',')}`)
                });

Browse More Popular Posts

Leave a Comment