使用 underscoreand 和 lodash 库按 ID 从数组中获取项目
Get item from array by ID using underscoreand and lodash library
我有这个数组:
var arrA = [
{"id": 1, "name": "Manager", "assignable": true},
{"id": 2, "name": "Developer", "assignable": true},
{"id": 3, "name": "Reporter", "assignable": true}
{"id": 4, "name": "Position", "assignable": true},
{"id": 5, "name": "Mayor", "assignable": true},
{"id": 6, "name": "Porter", "assignable": true}];
var arrB = [1,4,5];
我在我的项目中使用 underscore.js 和 lodash.js
从 arrA 数组中获取 id 等于 arrB 项的所有项的优雅方法是什么?
最高效和优雅的方法是使用 lodash 链接到 .indexBy() the items by ids, and then get the relevant items using .at():
var arrA = [
{"id": 1, "name": "Manager", "assignable": true},
{"id": 2, "name": "Developer", "assignable": true},
{"id": 3, "name": "Reporter", "assignable": true},
{"id": 4, "name": "Position", "assignable": true},
{"id": 5, "name": "Mayor", "assignable": true},
{"id": 6, "name": "Porter", "assignable": true}
];
var arrB = [1,4,5];
var returnedItems = _(arrA)
.indexBy('id')
.at(arrB)
.value();
document.getElementById('results').innerText = JSON.stringify(returnedItems);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
<div id="results"></div>
这是一个 lodash 解决方案:
_.filter(arrA, _.flow(
_.identity,
_.property('id'),
_.partial(_.includes, arrB)
));
filter() function returns a new array containing only the items we care about. We use the flow() 函数构建一个相应地过滤项目的回调 - 这是一个细分:
- identity() 函数 return 是传递给它的第一个参数。有几个参数传递给
filter()
回调,我们只关心第一个。
- property() 函数构建一个 return 给定 属性 的函数。在这种情况下,我们要比较
id
.
- 最后一步是检查给定的 id 是否存在于
arrB
中。为此,如果项目存在,我们使用 partial() to create a new function that uses includes() 到 return true。
这种风格并不适合所有人,但是紧凑的,而不是太粗俗。
我有这个数组:
var arrA = [
{"id": 1, "name": "Manager", "assignable": true},
{"id": 2, "name": "Developer", "assignable": true},
{"id": 3, "name": "Reporter", "assignable": true}
{"id": 4, "name": "Position", "assignable": true},
{"id": 5, "name": "Mayor", "assignable": true},
{"id": 6, "name": "Porter", "assignable": true}];
var arrB = [1,4,5];
我在我的项目中使用 underscore.js 和 lodash.js
从 arrA 数组中获取 id 等于 arrB 项的所有项的优雅方法是什么?
最高效和优雅的方法是使用 lodash 链接到 .indexBy() the items by ids, and then get the relevant items using .at():
var arrA = [
{"id": 1, "name": "Manager", "assignable": true},
{"id": 2, "name": "Developer", "assignable": true},
{"id": 3, "name": "Reporter", "assignable": true},
{"id": 4, "name": "Position", "assignable": true},
{"id": 5, "name": "Mayor", "assignable": true},
{"id": 6, "name": "Porter", "assignable": true}
];
var arrB = [1,4,5];
var returnedItems = _(arrA)
.indexBy('id')
.at(arrB)
.value();
document.getElementById('results').innerText = JSON.stringify(returnedItems);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
<div id="results"></div>
这是一个 lodash 解决方案:
_.filter(arrA, _.flow(
_.identity,
_.property('id'),
_.partial(_.includes, arrB)
));
filter() function returns a new array containing only the items we care about. We use the flow() 函数构建一个相应地过滤项目的回调 - 这是一个细分:
- identity() 函数 return 是传递给它的第一个参数。有几个参数传递给
filter()
回调,我们只关心第一个。 - property() 函数构建一个 return 给定 属性 的函数。在这种情况下,我们要比较
id
. - 最后一步是检查给定的 id 是否存在于
arrB
中。为此,如果项目存在,我们使用 partial() to create a new function that uses includes() 到 return true。
这种风格并不适合所有人,但是紧凑的,而不是太粗俗。