Underscore/Lodash 与从零开始
Underscore/Lodash vs. From Scratch
我是一名学生,最近开始使用 Underscore and/or Lodash,由于英语是我的第二语言,所以我有时很难理解这些 Underscore/Lodash 函数中的一些到底是什么做.
我对如何解决我目前正在处理的这个场景特别感兴趣...假设我有一个对象数组,而这些对象又包含一个文档数组;如果我想确定这些文档之一是否属于某种类型,如果是,则 return 文档关联的对象。所以在我的例子中,我正在处理一个发票对象数组,它又包含一个文档数组,为了解决这个问题,我使用了以下 JavaScript 代码:
let invoice = null;
for(let i = 0; i < $scope.invoices.length; i++){
let docs = $scope.invoices[i].documents;
if(docs && docs.length){
for(let j = 0; j < docs.length; j++) {
if(docs[j].type === 'document_xxxx'){
invoice = $scope.invoices[i];
break;
}
}
}
}
现在,我想了解完成同样事情的最简单方法,但使用 Underscore/Lodash。我怎样才能做到这一点?
使用Array#find with Array#some (or their lodash equivalents - .find and .some).
在 Array#find 的每次迭代中,使用 Array#some 迭代发票文档,如果其中一个具有正确的类型,Array#some returns true
和 Array#找到 returns 发票。
const invoices = [{ id: 1, documents: [{ type: 'document_mmmm' }] }, { id: 2, documents: [{ type: 'document_xxx1' }, { type: 'document_xxxx' }, { type: 'document_xx3x' }] }, { id: 3, documents: [{ type: 'document_zzz' }] }];
const result = invoices.find(({ documents = [] }) =>
documents.some((d) => d.type === 'document_xxxx'));
console.log(result);
使用下划线:
const invoices = [{ id: 1, documents: [{ type: 'document_mmmm' }] }, { id: 2, documents: [{ type: 'document_xxx1' }, { type: 'document_xxxx' }, { type: 'document_xx3x' }] }, { id: 3, documents: [{ type: 'document_zzz' }] }];
const result = _.find(invoices, ({ documents = [] }) =>
_.some(documents, (d) => d.type === 'document_xxxx'));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
我是一名学生,最近开始使用 Underscore and/or Lodash,由于英语是我的第二语言,所以我有时很难理解这些 Underscore/Lodash 函数中的一些到底是什么做.
我对如何解决我目前正在处理的这个场景特别感兴趣...假设我有一个对象数组,而这些对象又包含一个文档数组;如果我想确定这些文档之一是否属于某种类型,如果是,则 return 文档关联的对象。所以在我的例子中,我正在处理一个发票对象数组,它又包含一个文档数组,为了解决这个问题,我使用了以下 JavaScript 代码:
let invoice = null;
for(let i = 0; i < $scope.invoices.length; i++){
let docs = $scope.invoices[i].documents;
if(docs && docs.length){
for(let j = 0; j < docs.length; j++) {
if(docs[j].type === 'document_xxxx'){
invoice = $scope.invoices[i];
break;
}
}
}
}
现在,我想了解完成同样事情的最简单方法,但使用 Underscore/Lodash。我怎样才能做到这一点?
使用Array#find with Array#some (or their lodash equivalents - .find and .some).
在 Array#find 的每次迭代中,使用 Array#some 迭代发票文档,如果其中一个具有正确的类型,Array#some returns true
和 Array#找到 returns 发票。
const invoices = [{ id: 1, documents: [{ type: 'document_mmmm' }] }, { id: 2, documents: [{ type: 'document_xxx1' }, { type: 'document_xxxx' }, { type: 'document_xx3x' }] }, { id: 3, documents: [{ type: 'document_zzz' }] }];
const result = invoices.find(({ documents = [] }) =>
documents.some((d) => d.type === 'document_xxxx'));
console.log(result);
使用下划线:
const invoices = [{ id: 1, documents: [{ type: 'document_mmmm' }] }, { id: 2, documents: [{ type: 'document_xxx1' }, { type: 'document_xxxx' }, { type: 'document_xx3x' }] }, { id: 3, documents: [{ type: 'document_zzz' }] }];
const result = _.find(invoices, ({ documents = [] }) =>
_.some(documents, (d) => d.type === 'document_xxxx'));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>