【问题标题】:Filter JSON by key value with JavaScript使用 JavaScript 按键值过滤 JSON
【发布时间】:2021-08-26 06:15:24
【问题描述】:

给定 users.json 中的对象数组:

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

如何进行过滤以使最终结果为

console.log(result); // Bobby, your region subscriptions are: Netflix: (NA, EU), Prime Video: (NA)

谢谢!

编辑:我尝试使用 .filter() 和 .map() 方法,但到目前为止没有运气

【问题讨论】:

  • SO 不是免费的代码编写服务。到目前为止,您尝试过什么来自己解决这个问题?
  • 这显然无效,你有4个[和1个]
  • 过滤 JSON,将其转换为 JS 对象,然后过滤该 JS 对象,简直是一场噩梦。
  • @Teemu “过滤 JSON 简直是一场噩梦” - 你如何过滤字符串? o.O

标签: javascript arrays json filter


【解决方案1】:

您的 JSON 数据无效。不过,我想我已根据您的意图对其进行了调整。

这会将数据分解为按用户的格式,并提供一个函数来为任何指定用户输出订阅和区域。

const jsonString = `{"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"}]}`;

const obj = JSON.parse(jsonString);                                  // parse the data from a string into an object

const byUser = obj.value.reduce((acc, o) => {                        // reduce the "value" array to a single object
  if (!acc[o.user]) acc[o.user] = {};                                // create acc.userName if it does not exist
  if (acc[o.user][o.channel]) acc[o.user][o.channel].push(o.region); // if acc.userName.channelName has an array, add this region to it
  else acc[o.user][o.channel] = [o.region];                          // else create an array with this region in it
  return acc;                                                        // keep the created object for the next iteration
}, {});

function printForUser(userName) {                                    // create a function that takes a user's name
  const user = byUser[userName];                                     // take the user object from the byUser object
  return Object.keys(user).reduce((acc, k) => {                      // reduce the keys of the user object to one string
    return acc + ` ${k}: (${user[k].join(", ")}),`;                  // add on each subscription and regions
  }, `${userName}, your region subscriptions are:`).slice(0, -1);    // initialize the string and remove the last comma
}

console.log( printForUser("Bobby") );

【讨论】:

    【解决方案2】:

    你必须使用 lodash 来分组数据,然后映射数据

    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(',')}`)
                    });
    

    【讨论】:

    • “你必须使用 lodash 来对数据进行分组” - 不,OP 不能仅仅为此目的使用库。使用本机对象和方法很容易做到这一点。
    • 因为问题不清楚,所以我想他想为所有用户打印出来
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多