adding values and merging json objects based on key in d3js

I have a variable that looks something like this when I do console.log(variable1).

     0: {node01: "site05", amount: 1010}     1: {node01: "site02", amount: 1200}     2: {node01: "site08", amount: 880}     3: {node01: "site04", amount: 870}     4: {node01: "site01", amount: 650}     5: {node01: "site06", amount: 1370}     6: {node01: "site07", amount: 100}     7: {node01: "site09", amount: 210}     8: {node01: "site10", amount: 340}  

Another variable which looks like this in console.log(variable2):

     0: {node02: "site08", amount: 100}     1: {node02: "site02", amount: 200}     2: {node02: "site03", amount: 1050}     3: {node02: "site07", amount: 1300}     4: {node02: "site09", amount: 850}     5: {node02: "site10", amount: 890}     6: {node02: "site04", amount: 640}     7: {node02: "site06", amount: 290}     8: {node02: "site01", amount: 50}     9: {node02: "site05", amount: 200}     10: {node02: "site11", amount: 1060}  

I want to add the amount of both the variables based on the node value for example, “node01”: “site05” has amount:1010 and “node02”: “site05” has amount:200 the resultant variable should have “node”:”site05″ amount:1210. Also, “site11” doesn’t exist in variable1. so it should just have the value “node”:”site11″ amount:1060 –> this comes only from variable2

I tried using two nested for loops but it just results in nodes available in one variable. I am not sure how to map them all. Also, is there a solution using only d3js without using the for loops native to js? as from my understanding, D3 has some other approach to looping

Thanks in advance

Professor Asked on October 26, 2020 in Android langue.
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.