【发布时间】:2017-12-10 18:23:27
【问题描述】:
我有一个结构如下的对象:
{
guid x: { id: guid x, fooid: guid a, c: "baz"},
guid y: { id: guid y, fooid: guid a, c: "baz"},
guid z: { id: guid z, fooid: guid b, c: "baz"}
}
我想使用 fooid 作为键。但是有些对象有相同的 fooid 并且应该被分组(这里作为一个数组但也可以是对象):
{
guid a : [{ id: guid x, fooid: guid a, c: "bar"},{ id: guid y,
fooid: guid a, c: "bar"}],
guid b : { id: guid z, fooid: 3, c: "bar"}
}
我尝试使用 lodash 执行此操作,但没有成功,感谢任何帮助!试图寻找解决方案,但我不确定术语是什么,这使它非常困难。
【问题讨论】:
-
什么是
id (1)? -
你确定你不是从数组开始的吗?显示的语法对对象键无效,但对 "groupBy" 结果对象 有意义
-
我想表明key与内部对象中的id相同。不知道怎么表达才清楚? { guid x : { id: guid x, fooid: guid y, c: "bar"}, guid z : { id: guid z, fooid: guid y, c: "bar"}, guid k : { id: guid k, fooid: guid j, c: "bar"} } 这是一个对象,我试图简化的尝试可能搞砸了语法。
-
仍然不清楚你实际上是从什么开始的。你确定它是一个对象还是一个数组?
-
这是一个嵌套对象的对象,我敢肯定。那是故意的,并使用规范化器 (github.com/paularmstrong/normalizr) 创建。我更新了这个问题,我认为至少原始语法是正确的?
标签: javascript object lodash