【问题标题】:coffeescript - manipulating jsoncoffeescript - 操作 json
【发布时间】:2014-10-30 03:09:24
【问题描述】:

这只是逻辑问题,如果我有数据:

    [
        {
            from: 'user a',
            to: 'user b'
        },
        {
            from: 'user a',
            to: 'user c'
        },
        {
            from: 'user b',
            to: 'user d'
        },
        {
            from: 'user c',
            to: 'user d'
        },
        {
            from: 'user c',
            to: 'user d'
        }
    ]

我需要将这些数据操作到:

    [
        {
            from: 'user a',
            to: ['user b', 'user c']
        },
        {
            from: 'user b',
            to: ['user d']
        },
        {
            from: 'user c',
            to: ['user d']
        }
    ]

我使用了这个代码:

    result = []
    objTemp = obj
    from = []       
    obj.map (o) ->
        if o.from not in from
            from.push o.from
            to = []
            objTemp.map (oo) ->
                if oo.from is o.from and oo.to not in to
                    to.push oo.to
            temp =
                from: o.from
                to: to
            result.push temp

但结果不是我所期望的,同样的'from'中仍然有相同的'to':

   [
        {
            from: 'user a',
            to: ['user b', 'user c']
        },
        {
            from: 'user b',
            to: ['user d']
        },
        {
            from: 'user c',
            to: ['user d', 'user d'] <-- the problem
        }
    ]

你们是怎么用coffeescript解决的?

【问题讨论】:

    标签: javascript json coffeescript


    【解决方案1】:

    我会这样做:

    res = []
    obj.map (o) ->
      for r in res when r.from is o.from
        return (r.to.push o.to unless o.to in r.to)
      res.push from: o.from, to: [o.to]
    

    【讨论】:

    • 当你必须在 map 函数之外声明一个数组时,你知道你使用了错误的 map。我可以建议减少吗? jsfiddle.net/eaL6t1cn
    • @JedSchneider,我相信你是对的。但是你的小提琴有一个小问题,它返回重复的条目。如果我尝试在for 循环内使用return,则会引发错误。你是怎么处理的?
    • 对不起@multi 我没有调试准确性,只是向您展示了如何使用您的方法传入数组以减少。我可以更深入地研究一下并提供答案。我没有意识到它呈现的结果不准确。
    • 更新了我自己的答案。要回答您的问题,您需要对结果进行 uniq。
    【解决方案2】:

    为了方便使用groupByuniq 方法(我猜还有chain),我可能会使用下划线或lowdash。如果您对它们的实现感兴趣以便在原始咖啡脚本中完成,您可以查看带注释的源代码。

    http://underscorejs.org/docs/underscore.html

    chain/value 只允许我调用这些内联而不是让临时变量来保存它们或使函数调用不那么明显。

    fromUser = (x)-> x.from
    
    toRelationships = (memo, items, index, list)->
      key = _(items).pluck('from')[0]
      vals = _.values(list[key])
      memo[key] = _(vals).chain().pluck('to').uniq().value()
      memo
    
    result = _(data).chain().groupBy(fromUser).reduce(toRelationships, []).value()
    
    # without chain
    grouped = _(data).groupBy( fromUser )
    result = _(grouped).reduce( toRelationships, [])
    

    jsfiddle:http://jsfiddle.net/eaL6t1cn/2/

    【讨论】:

      【解决方案3】:

      这是我的看法,我现在为这没有像我希望的那样简洁而道歉。如果我使用 jQuery(或其他第三方),这会更简单一些,但这里没有任何库。

      a =     [
              {
                  from: 'user a',
                  to: 'user b'
              },
              {
                  from: 'user a',
                  to: 'user c'
              },
              {
                  from: 'user b',
                  to: 'user d'
              },
              {
                  from: 'user c',
                  to: 'user d'
              },
              {
                  from: 'user c',
                  to: 'user d'
              }
          ]
      c = {}
      d = []
      (value for value in a).forEach (v) -> 
           c[v.from] = do -> if not c[v.from] then [v.to] else ("#{c[v.from]},#{v.to}").
               split(',').reduce ((p,c) -> 
                   if c not in p then p.concat([c]) else [c] ), [] 
      
      d.push {'from':v,'to':k} for v,k of c
      
      console.log d
      

      【讨论】:

        猜你喜欢
        • 2016-08-31
        • 2012-06-21
        • 2016-01-28
        • 2018-05-20
        • 2016-11-05
        • 2016-04-04
        • 2021-04-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多