我有两个阵列,余额和提交
balances = [
{name: 'vacation', value: ''21},
{name: 'account', value: '200'},
{name: 'order', value: '15'},
]
commits = [
{balanceName: 'vacation', paramName: 'number of days'},
{balanceName: 'order', paramName: 'number of items'}
]
我想删除未提交的表单余额,在此示例中,我想从余额中删除帐户值,因为它不在任何提交的balanceName中 我怎么能通过下划线或任何其他库
来做到这一点答案 0 :(得分:2)
你可以使用var balances = [
{name: 'vacation', value: '21'},
{name: 'account', value: '200'},
{name: 'order', value: '15'}
]
var commits = [
{balanceName: 'vacation', paramName: 'number of days'},
{balanceName: 'order', paramName: 'number of items'}
]
balances2=
_.filter(balances,(b)=>commits.map(c=>c.balanceName).indexOf(b.name)>-1);
console.log("using shorter syntax");
console.log(balances)
//OR
balances=
_.filter(balances,function(b){return commits.map(function(c){return c.balanceName}).indexOf(b.name)>-1});
console.log("ES5 syntax");
console.log(balances2)
这样做:
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
_.filter
&#13;
_.find
将返回符合条件的所有条目。例如_.map
将只返回第一个条目。
我还使用了c=>c.balanceName
,它通过使用transform函数(在我的例子<?php
// We'll be outputting a PDF
header('Content-Type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
中)将具有多个属性的数组转换为其他数组。
答案 1 :(得分:-1)
不需要图书馆。
function contains(commits, name)
{
commits.some(function(x) { return x.balanceName === name});
}
for(blance in balances) {
if (contains(commits, balance.name){
// Remove balance
}
}