1

i have 2 array like this :

let payrollname = [{"code": "a1", "name": "Loan A"}, {"code": "a2", "name": "Loan B"}, {"code": "a3", "name": "Loan C"}, {"code": "a4", "name": "Loan D"}, {"code": "a5", "name": "Loan E"}]

and

let payrollbalance = [{"a1": 0, "a2": 100000, "a3": 500000, "a4": 300000, "a5": 45900}]

how can i pushing values from payrollbalance to payrollname ?

i mean push like this, values from payrollbalance.a4 which mean 300000 push to payrollname.code == a4

and i hope final results can similar like :

let result = `[{"code": "a1", "name": "Loan A", "values": 0}, {"code": "a2", "name": "Loan B", , "values": 100000}, {"code": "a3", "name": "Loan C", "values": 500000}, {"code": "a4", "name": "Loan D", "values": 300000}, {"code": "a5", "name": "Loan E",  "values": 45900}]`

any help will be appricated, im already messing up whole days.

thanks in advance

6
  • Is there a reason payrollbalance is an array? Can it have more than one object? Commented Dec 28, 2020 at 8:15
  • @Nick Parsons no, only 1 length . and always. Commented Dec 28, 2020 at 8:16
  • Ok, can you also add your attempts to far (even if they don't work?) then you can see where you went wrong Commented Dec 28, 2020 at 8:17
  • @NickParsons im trying to manipulate render at front end (because i cant change their backend) like this.state.payrollbalance+'.a4' but return always be [object Object].a4 Commented Dec 28, 2020 at 8:21
  • 1
    @Tumbalakun Better to share attempt for such question and find out what went wrong instead of expecting someone to solve it for you. Till now you have been messing up whole day, If someone solve it for you then you will be messing up for whole life. Commented Dec 28, 2020 at 8:21

3 Answers 3

1

Use Array.map() and add values key with the value of payrollbalance[0] with indicating the json key by [] for dynamic.

let payrollname = [{"code": "a1", "name": "Loan A"}, {"code": "a2", "name": "Loan B"}, {"code": "a3", "name": "Loan C"}, {"code": "a4", "name": "Loan D"}, {"code": "a5", "name": "Loan E"}]

let payrollbalance = [{"a1": 0, "a2": 100000, "a3": 500000, "a4": 300000, "a5": 45900}]

let result = payrollname.map(item => ({
  ...item,
  values: payrollbalance[0][item.code]
}));

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

0

Make an array seach, find the index and change it after

let payrollname = [{"code": "a1", "name": "Loan A"}, {"code": "a2", "name": "Loan B"}, {"code": "a3", "name": "Loan C"}, {"code": "a4", "name": "Loan D"}, {"code": "a5", "name": "Loan E"}]
let payrollbalance = [{"a1": 0, "a2": 100000, "a3": 500000, "a4": 300000, "a5": 45900}]
var result = [];
let pblance = payrollbalance[0]
for(let p in pblance)
{
  let index = payrollname.findIndex(el => el.code==p)
  let pname = payrollname[index]
  pname.values = pblance[p];
  result.push(pname)
}
console.log(result);

4 Comments

since you did findindex inside for() loop, the o notation will be o(n*n), that the performance is not good.
@WilliamWang i should use .indexOf instead of findIndex
no, it's same. indexOf also iterate the array.
you can just call the object dynamic key using []. refer to above my answer.
0

this code will work !

let payrollname = [{"code": "a1", "name": "Loan A"}, {"code": "a2", "name": "Loan B"}, {"code": "a3", "name": "Loan C"}, {"code": "a4", "name": "Loan D"}, {"code": "a5", "name": "Loan E"}]
let payrollbalance = [{"a1": 0, "a2": 100000, "a3": 500000, "a4": 300000, "a5": 45900}]
let result = []
let i = 0
payrollname.forEach(element => {
    let value = payrollbalance[0][element.code]
    element.values = value;
    result[i] = element
    i=i+1
});
console.log(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.