cakephp函数像JavaScript中的Set :: combine [英] cakephp functions like Set::combine in Javascript

查看:229
本文介绍了cakephp函数像JavaScript中的Set :: combine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一些javascript函数与浏览器中客户端的cakephp函数Set :: combine和Set :: classicExtract相同?
http://book.cakephp.org/2.0 /en/core-utility-libraries/set.html



这样我就可以使用这个数组:

  a = [
{
'User':{
'id':2,
'group_id'
'name':'Alfred'
}
},
{
'User':{
'id':12,
'group_id':2,
'name':'Albert'
}
}
]

使用

  Set :: combine(a,'{n} .User.id','{n} .User.name')

p>

  a = {
2:'Alfred',
12:'Albert'
} b $ b

但是这不仅仅是为了这个简单的例子,我真的在寻找一个库或其他类似cakephp的方法。

strong>看到一个相当于CakePHP的Hash类的JS,但遗憾的是在一个小时的谷歌搜索后,我找不到任何东西... Underscore.js和Sugar.js都有一些过滤功能,但似乎都不工作的多维数组。



在蛋糕我经常做这样的事情。

  Hash :: extract($ users,'{n} .User.Posts。{n} .Comments。{n} [published = 1]'); 

如果任何人知道一个JS库这样做,将非常感谢提示。 >




编辑:我已经找到了一些选项,允许这种查询JSON数据




  • JSPath

    .automobiles {.maker ===Honda&& .year> 2009} .model


  • json :select()(受CSS选择器的启发)

    .automobiles .maker:val(Honda).model p>


  • JSONPath (受XPath启发更多)

    $。automobiles [?(@。maker ='Honda')]。model




我认为JSPath看起来最好,所以我将尝试并将其与我的AngularJS + CakePHP应用程序集成。


Are there already some javascript functions that do the same as the cakephp functions Set::combine and Set::classicExtract on client side in the browser? http://book.cakephp.org/2.0/en/core-utility-libraries/set.html

So that I can bring this array:

a = [
   {
        'User' : {
            'id' : 2,
            'group_id' : 1,
            'name' : 'Alfred'
         }
    },
    {
        'User' : {
            'id' : 12,
            'group_id' : 2,
            'name' : 'Albert'
         }
    }
]

with a function like

Set::combine(a, '{n}.User.id', '{n}.User.name') 

into the format

a = {
  2 : 'Alfred',
  12 : 'Albert' 
}

for example. But it's not only for this simple example, I'm really looking for a library or something which can do it in a similar way to cakephp. Thanks a lot in advance!

解决方案

I would love to see a JS equivalent to CakePHP's Hash class, but sadly after an hour of Googling, I cannot find anything... Underscore.js and Sugar.js both have some filtering functions, but neither seem to work on multi-dimensional arrays.

In Cake I often do things like this...

Hash::extract($users, '{n}.User.Posts.{n}.Comments.{n}[published=1]');

If anyone knows a JS library that does this, would be very grateful for the tip.


EDIT: I have since found a few options that allow this kind of querying of JSON data (with examples of the syntax they use).

  • JSPath
    .automobiles{.maker === "Honda" && .year > 2009}.model

  • json:select() (inspired more by CSS selectors)
    .automobiles .maker:val("Honda") .model

  • JSONPath (inspired more by XPath)
    $.automobiles[?(@.maker='Honda')].model

I think JSPath looks the nicest, so I'm going to try and integrate it with my AngularJS + CakePHP app.

这篇关于cakephp函数像JavaScript中的Set :: combine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆