0

I am beginner in data structure and trying to improve my skills. I am trying to divide the object value to list of other object. So I/P is

1st Object JSON:

let mapMonth ={
    "10": 8,
    "11": 30,
    "12": 31,
    "01": 23
}

where 10 is Oct, 11 is Nov, 12 is Dec and 01 is Jan.

2nd Object JSON:

let mapData = {
    "key1": {
        "subkey1": [
            [407341537, 1666737463, 363248139, 596560162]
        ],
        "subkey2": ["Oct", "Nov", "Dec", "Jan"]
    },
    "key2": {
        "subkey1": [
            [78491802, 334718068, 68299710, 81365082]
        ],
        "subkey2": ["Oct", "Nov", "Dec", "Jan"]
    },
    "key3": {
        "subkey1": [
            [501844, 3362217, 648527, 1073573]
        ],
        "subkey2": ["Oct", "Nov", "Dec", "Jan"]
    }
}

So now I need to divide 407341537 with 8 i.e 50917692.125, 1666737463 with 30 i.e 55557915.4333 and so on.. Expected output:

{
    "key1": {
        "subkey1": [
            [50917692.125, 55557915.4333, 11717681.9,72466846.2174]
        ],
        "subkey2": ["Oct", "Nov", "Dec", "Jan"]
    },
    "key2": {
        "subkey1": [
            [9811475.25, 11157268.9333, 2203216.45161, 3537612.26087]
        ],
        "subkey2": ["Oct", "Nov", "Dec", "Jan"]
    },
    "key3": {
        "subkey1": [
            [62730.5, 112073.9, 20920.225, 46677.086]
        ],
        "subkey2": ["Oct", "Nov", "Dec", "Jan"]
    }
}

Code I have tried:

let averageObj = {};
var count = 0;
for (let key in mapData) {
  averageObj[key] = [];
  mapData[key]['subkey1'][0].forEach((data, index) => {
    for (let monthKey in mapMonth) {
      averageObj[key].push(data / mapMonth[monthKey]);
    }
  });
}

Please let me know if you need anything else.

2
  • So, in short: the mapMonth["10"] value should be used to divide the value of mapData.keyX.subkeyX[0], is that correct? Commented Jan 23, 2020 at 17:09
  • Yes sir!! You're right Commented Jan 23, 2020 at 17:10

1 Answer 1

1

Assuming an object with fixed keys and nested array with only one index:

This approach mutates the original object.

let mapMonthDesc = {"Oct": "10","Nov": "11","Dec": "12","Jan": "01"},
    mapMonth = {"10": 8,"11": 30,"12": 31,"01": 23},
    mapData = {"key1": {"subkey1": [[407341537, 1666737463, 363248139, 596560162]],"subkey2": ["Oct", "Nov", "Dec", "Jan"]},"key2": {"subkey1": [[78491802, 334718068, 68299710, 81365082]],"subkey2": ["Oct", "Nov", "Dec", "Jan"]},"key3": {"subkey1": [[501844, 3362217, 648527, 1073573]],"subkey2": ["Oct", "Nov", "Dec", "Jan"]}};

Object.values(mapData).forEach(({subkey1: [numbers], subkey2}) => {
  numbers.forEach((n, i) => numbers[i] = n / mapMonth[mapMonthDesc[subkey2[i]]]);
});

console.log(mapData);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

Great !! Thank you can you please add comment so I can understand better

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.