我有以下包含数组的对象posts:
var posts = {
"posts": [{
"id": 83,
"title": "Onfido helps iCracked win customers’ confidence",
"slug": "onfido-helps-icracked-win-customers-confidence",
"tags": [{
"id": 25,
"slug": "case-studies"
}, {
"id": 10,
"slug": "industry-news"
}]
}, {
"id": 82,
"title": "Machine learning makes banking more personal",
"slug": "machine-learning-makes-banking-more-personal",
"tags": [{
"id": 10,
"slug": "industry-news"
}]
}, {
"id": 81,
"title": "11 reasons to join Onfido",
"slug": "ten-reasons-to-join-onfido",
"tags": [{
"id": 100,
"slug": "our-expertise"
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
使用Lodash,我想提取所有tags.slug有价值的帖子"case-studies".目前我一直在尝试以下运气:
_.filter(posts, …Run Code Online (Sandbox Code Playgroud) 我有一个Home包含内容的主页,例如模型intro_copy,about_image和about_copy.
在Home模型上,我还希望能够Post使用has_one关系从我的模型中发布3个帖子.基本上只是使用id链接它们.
我的Home架构如下所示:
schema "home" do
field :intro_copy, :string
field :about_copy, :string
field :about_image, Image.Type
has_one :post_1, Post
has_one :post_2, Post
has_one :post_3, Post
timestamps()
end
Run Code Online (Sandbox Code Playgroud)
我的changeset功能看起来像这样:
def changeset(struct, params \\ %{}) do
struct
|> cast_assoc(params, [:post_1, :post_2, :post_3])
|> cast(params, @required_fields, @optional_fields)
end
Run Code Online (Sandbox Code Playgroud)
此外,在我的迁移中,我将以下行添加到:home表中:
add :post_1_id, references(:posts)
add :post_2_id, references(:posts)
add :post_3_id, references(:posts)
Run Code Online (Sandbox Code Playgroud)
有什么地方我明显错了吗?
我收集了如下所示的UNIX时间戳:
[
{"start_time":1540458000000, "end_time":1540472400000},
{"start_time":1540458000000, "end_time":1540486800000},
{"start_time":1540458000000, "end_time":1540501200000},
{"start_time":1540472400000, "end_time":1540486800000},
{"start_time":1540472400000, "end_time":1540501200000},
{"start_time":1540486800000, "end_time":1540501200000}
]
Run Code Online (Sandbox Code Playgroud)
我想从两个挑选出所有的独特的价值观start_time和end_time,所以我留下了:
[
{"start_time":1540458000000},
{"start_time":1540472400000},
{"start_time":1540486800000}
{"end_time":1540472400000},
{"end_time":1540486800000},
{"end_time":1540501200000},
]
Run Code Online (Sandbox Code Playgroud)
我看使用类似的东西使用groupBy,pluck,zipObj多使用的答案在这里。但不幸的是没有运气。
最好的是ramda函数,该函数无需指定特定键即可工作。
我有一个checkout-form具有像一些动作成分next,previous,submitForm和selectDate。目前我只能执行这样yield的selectDate操作:
{{!-- checkout-form.js --}}
<div class='checkout-form'>
{{yield (action 'selectDate')}}
</div>
Run Code Online (Sandbox Code Playgroud)
我希望能够checkout-form像这样使用我的组件:
{{!-- order.hbs --}}
{{#checkout-form as |submitForm, selectDate|}}
{{checkout-field placeholder="Full Name" value=model.order.name}}
{{!-- another field component that uses selectDate --}}
{{checkout-form-actions action=submitForm}}
{{/checkout-form}}
Run Code Online (Sandbox Code Playgroud)
我将如何产生要在我的内部使用的多个操作checkout-form.hbs?
我想比较两个技能列表,以提供一个分数出现在另一个列表中的百分比分数:
user_skills = [
%{name: "Elixir"},
%{name: "Python"}
]
project_skills = [
%{name: "Elixir"},
%{name: "Erlang"},
%{name: "Ruby"}
]
Run Code Online (Sandbox Code Playgroud)
user_skills出现的百分比是project_skills多少?我们想要的是的结果50%。
I\xe2\x80\x99d 就像一个由 26 个数字组成的数组,从 开始1到结束42。
我还没有\xe2\x80\x99t 能够找到如何做到这一点(答案似乎是查找两个数字与指定步数之间的所有整数)。数字可以是浮点数。
\n\n理想情况下,我\xe2\x80\x99d 希望将其作为可重用的函数,例如:
\n\nnumberRange(lower, upper, steps)\nRun Code Online (Sandbox Code Playgroud)\n\n举一个简单的例子,如果我这样做的话:
\n\nnumberRange(2, 10, 5)\nRun Code Online (Sandbox Code Playgroud)\n\n它将返回:[2, 4, 6, 8, 10]。
我想知道如何回应当前的high_voltage页面名称作为我的类<body>?
我目前的解决方案有点麻烦:
# app/views/layouts/application.html.erb
<body class="<%= yield :body_class %>">
# app/views/pages/principles.html.erb
<% content_for :body_class do %>home<% end %>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
我目前有一个user对象,它有一个currentRoom()方法,有时可能不存在或返回null.
如果currentRoom()方法返回一些东西,那么我需要调用一个messages()方法.如果从任何一个都没有返回任何内容,我想返回一个默认的空数组[].
我想使用Ramda在功能上解决这个问题,所以它很整洁且可重复使用.目前,我的(非Ramda)代码如下所示:
const user = Users.findOne({ username: params.id })
const room = (user.currentRoom && user.currentRoom() || {})
const messages = (room.messages && room.messages() || [])
Run Code Online (Sandbox Code Playgroud)
我想的某种逻辑是传递所需方法的列表,以及默认结果,如果没有任何结果.
/* getMessages(defaultIfNull, methodsArray, object) */
getMessages([], ['currentRoom', 'messages'], user)
Run Code Online (Sandbox Code Playgroud)
基本上有点类似的东西pathOr,但对于对象上的方法.
我正在创建一个简单的服务,它接收一个电子邮件地址 - 并从用户列表中找到用户.
这是一个包含用户列表的简化版本.我想根据用户的电子邮件地址提取用户.
def endpoint do
[%{email: "foo@example.org", account_type: "full"},
%{email: "bar@earxample.org", account_type: "standard"},
%{email: "baz@example.org", account_type: "full"}]
end
def get_by_email(user, email) do
user |> Map.get(:email)
end
def dev_endpoint(email) do
endpoint
|> Enum.map(&get_by_email(email)/1)
end
def show(conn, %{"id" => email}) do
response = dev_endpoint(email)
json(conn, %{"email" => response})
end
Run Code Online (Sandbox Code Playgroud)
基本上:
dev_endpoint("foo@example.org")
Run Code Online (Sandbox Code Playgroud)
应该返回:
%{email: "foo@example.org", account_type: "full"}
Run Code Online (Sandbox Code Playgroud)
我知道我的捕获语法有问题,但我尝试了各种不同的迭代,没有运气.
我有以下数据结构,其中包含Date对象,其中一些嵌套在对象中:
[
new Date("2018-11-20T09:00:00.000Z"),
new Date("2018-11-19T09:00:00.000Z"),
{
"before": new Date("2018-12-14T00:00:00.000Z")
}
]
Run Code Online (Sandbox Code Playgroud)
使用ramda,我想将它弄平,使其变为:
[
new Date("2018-11-20T09:00:00.000Z"),
new Date("2018-11-19T09:00:00.000Z"),
new Date("2018-12-14T00:00:00.000Z")
]
Run Code Online (Sandbox Code Playgroud)
我使用的合并审理map,values,unnest和flatten,但没有运气.理想情况下,我喜欢这样做,以便无论嵌套对象和键名如何都会变平.
我想对自己进行过滤(以对数组中的所有其他项目进行测试):
const people = [{
name: "James Cromwell",
region: "Australia"
}, {
name: "Janet April",
region: "Australia"
}, {
name: "David Smith",
region: "USA"
}, {
name: "Tracey Partridge",
region: "USA"
}]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我想做的就是留给以下人员:
name以相同的字母开头region 值是一样的在这种情况下,结果将是:
[{
name: "James Cromwell",
region: "Australia"
}, {
name: "Janet April",
region: "Australia"
}]
Run Code Online (Sandbox Code Playgroud)
我一直在看结合做filter,any但没有喜悦。我在这里使用ramda的决定是,我在现有的ramda compose函数中使用它来转换数据。