我有2个对象列表:
people =
[{id: 1, name: "Tom", carid: 1},
{id: 2, name: "Bob", carid: 1},
{id: 3, name: "Sir Benjamin Rogan-Josh IV", carid: 2}];
cars=
[{id: 1, name: "Ford Fiesta", color: "blue"},
{id: 2, name: "Ferrari", color: "red"},
{id: 3, name: "Rover 25", color: "Sunset Melting Yellow with hints of yellow"}];
Run Code Online (Sandbox Code Playgroud)
是否有一个函数(可能在Angular,JQuery,Underscore,LoDash或其他外部库中)在这些函数的一行中进行左连接?就像是:
peoplewithcars = leftjoin( people, cars, "carid", "id");
Run Code Online (Sandbox Code Playgroud)
我可以编写自己的,但如果LoDash有一个优化版本,我想使用它.
这个问题是针对lodash的.
给定两个对象数组,使用另一个数组的对象过滤一个数组的最佳方法是什么?我试图在下面提出一个场景,我这样做的方式是使用两个.forEach循环,但我想知道如果使用lodash,有更好的方法来进行这种类型的过滤.
users.
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'joe', 'age': 40, 'active': false },
{ 'user': 'fred', 'age': 50, 'active': false },
{ 'user': 'fred', 'age': 60, 'active': false },
{ 'user': 'fred', 'age': 70, 'active': false },
{ 'user': 'fred', 'age': 22, 'active': false },
{ 'user': 'fred', 'age': 25, 'active': false },
{ 'user': 'barney', 'age': 40, 'active': false }, …Run Code Online (Sandbox Code Playgroud) 我有这个:
[ { list:
[ [Object],
[Object] ] },
{ head:
[ [Object],
[Object] ] }
]
Run Code Online (Sandbox Code Playgroud)
并希望将其变成这样:
{ list:
[ [Object],
[Object] ],
head: [ [Object],
[Object] ]
}
Run Code Online (Sandbox Code Playgroud)
所以一个对象数组到一个对象中.用lodash实现这一目标会很棒.
如何检查数组的所有元素是否真实或虚假.
由于以下似乎没有这样做:
_.all([true, true, true], true);
它返回:false?
假设我有这个对象(或这些对象的数组):
var person = {
birth: {
place: {
country: 'USA'
}
}
};
Run Code Online (Sandbox Code Playgroud)
我以为有一个lodash函数,我可以传入'birth.place.country'并获取值USA.
在lodasdh 3.x中有这样的功能,还是我想象这个?
我刚开始使用lodash并拥有这个对象数组,其中一个属性有一个整数或为null.我知道如何过滤数组中的null项,但我如何检查它是否为空?
假设我有这样的事情:
var users = [
{ 'user': 'barney', 'default': 1 },
{ 'user': 'dino', 'default': 0 },
{ 'user': 'wilma', 'default': 1 },
{ 'user': 'fred', 'default': null }
];
Run Code Online (Sandbox Code Playgroud)
然后我想要这样的东西:
var notNullDefault = _.filter(sourceData, ['is_default', ! null ]); // objects with names barney, dino, wilma
var nullDefault = _.filter(sourceData, ['is_default', null ]); // object with name fred
Run Code Online (Sandbox Code Playgroud)
再一次,我是lodash的新手,所以也许有更好的方法来实现这一点.
提前致谢.
我想使用,memoize但我担心缓存会无限增长,直到发生悲伤.
我无法通过google/stackoverflow搜索找到任何内容.
PS我正在使用lodash v4.
我有一个这样的对象数组:
[ {"name": "apple", "id": "apple_0"},
{"name": "dog", "id": "dog_1"},
{"name": "cat", "id": "cat_2"}
]
Run Code Online (Sandbox Code Playgroud)
我想插入另一个也被命名的元素,apple因为我不想在那里重复,我怎么能用lodash来查看数组中是否有一个具有相同名称的对象?
我的对象看起来像这样:
const features = [{
'name': 'feature1', 'tags':
[{'weight':10, 'tagName': 't1'},{'weight':20, 'tagName': 't2'}, {'weight':30, 'tagName': 't3'}]
},
{
'name': 'feature2', 'tags':
[{'weight':40, 'tagName': 't1'}, {'weight':5, 'tagName':'t2'}, {'weight':70, 'tagName':'t3'}]
},
{
'name': 'feature3', 'tags':[
{'weight':50, 'tagName': 't1'}, {'weight':2, 'tagName': 't2'}, {'weight':80, 'tagName': 't3'}]
}]
Run Code Online (Sandbox Code Playgroud)
我希望我的输出看起来像这样:
const features = [{'name':'feature1', 'weight':10, 'tagName':'t1'},
{'name':'feature1', 'weight':20, 'tagName':'t2'}, ...
{'name':'feature3', 'weight':80, 'tagName':'t3'}]
Run Code Online (Sandbox Code Playgroud)
我试过了merge,flatten但它不起作用.
更新1 我试过这个:
let feat = features;
results = []
_.each(feat, (item) => {
console.log(item);
results.push(_.flatten(_.pick(item.tags, 'weight'))); // pick …Run Code Online (Sandbox Code Playgroud) 我一直在关注有关Lodash的文章,“ 为什么使用_.chain是一个错误”,它着重指出您可以使用来消除对链条的需求Flow。
给出的示例是以下使用链
import _ from "lodash";
_.chain([1, 2, 3])
.map(x => [x, x*2])
.flatten()
.sort()
.value();
Run Code Online (Sandbox Code Playgroud)
可以使用流程转换为以下内容
import map from "lodash/fp/map";
import flatten from "lodash/fp/flatten";
import sortBy from "lodash/fp/sortBy";
import flow from "lodash/fp/flow";
flow(
map(x => [x, x*2]),
flatten,
sortBy(x => x)
)([1,2,3]);
Run Code Online (Sandbox Code Playgroud)
但是,当我使用TypeScript实现此功能时,出现以下错误
对象的类型为'unknown'.ts(2571)
有什么方法可以解决此问题,以便TypeScript知道它正在处理哪种类型?