javascript - underscorejs how to compare between two arrays? -


i have 2 arrays, balances , commits

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'} ] 

i want remove form balances not in commits, in example want remove account value balances cause not in of balancename of commits how can underscore or other library

you can using undescore.js this:

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>

_.filter return entries match criteria. _.find, instance return first entry. used _.map converts array multiple properties other array using transform function (in case c=>c.balancename).


Comments

Popular posts from this blog

html - How to custom Bootstrap grid height? -

javascript - pass values from mssql to views in node -

ruby - unknown property method: 'wait' on EC2 windows server Instance -