所以在 lodash
文档上,这就是我对范围的理解,_.range(start, end)
。
因此,如果我对 JSON 对象数组使用 .range()
。喜欢...
const arr = [
{
name: 'name',
},
];
假设我有 20 个对象并且我执行了 arr._.range(5, 5);
我会返回第 5 个 JSON 对象并从那里返回 5 个。
所以我创建了一个映射函数,它返回一个 JSON 对象列表,然后我对它们使用 range
,这就是我所拥有的:
import R from 'lodash';
const getJsonData = (offset, limit) => (
R.map([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], x => ({
title: `title ${x}`,
subtitle: 'description',
image_url: 'http://some-img-url.com',
date: 'date',
tag: 'tag',
places: [
{
places: 'places',
},
],
}), R.range(offset, (limit + offset)))
);
所以我将其称为 getJsonData(5, 5);
,但它仍然会返回包含 20 个 JSON 对象的完整列表。
我是否误解了 range
的工作原理?
最佳答案
你得到的结果是正确的。
lodash
中的 map
函数不接受第三个参数。
所以 range
调用没有被考虑在内。
https://lodash.com/docs/4.17.4#map
所以,我建议你像这样使用 slice
函数:
import R from 'lodash';
const getJsonData = (offset, limit) => (
R.slice(
R.map([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], x => ({
title: `title ${x}`,
subtitle: 'description',
image_url: 'http://some-img-url.com',
date: 'date',
tag: 'tag',
places: [
{
places: 'places',
},
],
}))
, offset, limit + offset)
);
关于JSON 对象数组上的 Javascript lodash 范围不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44562965/