function - Javascript functional logic not working -
i trying create object
currency
opening balance
and closing balance
i have following data
var _ = require('underscore'); var accounts=[ { "id":"20", "name":"oasis mall-rmb account", "branchid":"6", "type_id":"1", "currency_id":"1", "balance":"0", "createdby":"2", "active":"1", "ts":"2017-06-30 00:12:08" }, { "id":"21", "name":"oasis mall-usd account", "branchid":"6", "type_id":"1", "currency_id":"2", "balance":"0", "createdby":"2", "active":"1", "ts":"2017-06-30 00:12:09" }, { "id":"22","name":"oasis mall-dirham account", "branchid":"6", "type_id":"1", "currency_id":"3", "balance":"0", "createdby":"2", "active":"1", "ts":"2017-06-30 00:12:09" }, { "id":"23", "name":"oasis mall-ugx account", "branchid":"6", "type_id":"1", "currency_id":"4", "balance":"0", "createdby":"2", "active":"1", "ts":"2017-06-30 00:12:09" }, { "id":"24", "name":"oasis mall-ksh account", "branchid":"6", "type_id":"1", "currency_id":"5", "balance":"0", "createdby":"2", "active":"1", "ts":"2017-06-30 00:12:09" } ] var balances =[ { "id":"101", "branchid":"6", "accountid":"20", "openingbalance":"3260000", "closingbalance":"3260000", "date":"2017-08-15", "ts":"2017-08-15 02:27:08" }, { "id":"102", "branchid":"6", "accountid":"21", "openingbalance":"1882492", "closingbalance":"1882492", "date":"2017-08-15", "ts":"2017-08-15 02:27:08" }, { "id":"103","branchid":"6", "accountid":"22", "openingbalance":"-3000", "closingbalance":"-3000", "date":"2017-08-15", "ts":"2017-08-15 02:27:08" }, { "id":"104", "branchid":"6", "accountid":"23", "openingbalance":"95199318", "closingbalance":"95199318", "date":"2017-08-15", "ts":"2017-08-15 02:27:08" }, { "id":"105", "branchid":"6", "accountid":"24", "openingbalance":"1847000", "closingbalance":"1847000", "date":"2017-08-15", "ts":"2017-08-15 02:27:08" } ] i know how in loops , if statements. want functional way have tried writing code.
var displayaccounts = function(accounts,balances){ var displayaccounts = _(accounts).map((x)=>{ var mybalance = _(balances) .reduce((acc,z)=>{ acc={ currency :x.currency_id, openingbalance : z.openingbalance, closingbalance : z.closingbalance }; return acc; },{}); return mybalance; }); return displayaccounts; } console.log(displayaccounts(accounts,balances)); but gives me answer dont like. gives me
[ { currency: '1', openingbalance: '1847000', closingbalance: '1847000' }, { currency: '2', openingbalance: '1847000', closingbalance: '1847000' }, { currency: '3', openingbalance: '1847000', closingbalance: '1847000' }, { currency: '4', openingbalance: '1847000', closingbalance: '1847000' }, { currency: '5', openingbalance: '1847000', closingbalance: '1847000' } ] the same opening , closing balances throught. need each currency corresponding opening , closing balances .
[ { currency: '1', openingbalance: '3260000', closingbalance: '3260000' }, { currency: '2', openingbalance: '1882492', closingbalance: '1882492' }, { currency: '3', openingbalance: '-3000', closingbalance: '-3000' }, { currency: '4', openingbalance: '95199318', closingbalance: '95199318' }, { currency: '5', openingbalance: '1847000', closingbalance: '1847000' } ] how can ?
this not pure functional approach generates desired result:
function displayaccounts(acc, bal) { return acc.map(x => { let b = bal.find(b => b.accountid === x.id); return { currency: x.currency_id, openingbalance: b.openingbalance, closingbalance: b.closingbalance } }); } console.log(displayaccounts(accounts, balances));
Comments
Post a Comment