Split the data in to 4 different categories

81 views Asked by At

For example - I have the following data This below values are in an array it is not key and value.

tier2_rightend: 10766
tier1_rightend: 10766
tier1_leftend: 2719
tier1_leftstart: 1
tier2_maxjunctions: 2
tier2_leftend: 2719
tier2_leftstart: 1
tier2_minjunctions: 1
tier2_rightstart: 10275
tier1_minjunctions: 2
tier1_maxjunctions: 2
tier1_rightstart: 10275

I need the result in following format

tier1 = tier1_leftstart(value)  -  tier1_leftend(value) ,tier1_rightstart(value) -tier1_rightend(value)

So I need to have 4 arrays as tier1 left start tier1 left end Tier1 right start and tier1 right end

Also I need similar thing to be done for tier 2

Can can body help?

1

There are 1 answers

6
Nina Scholz On

You could take two nested arrays for the key parts and create a new object with the result.

var object = { tier2_rightend: 10766, tier1_rightend: 10766, tier1_leftend: 2719, tier1_leftstart: 1, tier2_maxjunctions: 2, tier2_leftend: 2719, tier2_leftstart: 1, tier2_minjunctions: 1, tier2_rightstart: 10275, tier1_minjunctions: 2, tier1_maxjunctions: 2, tier1_rightstart: 10275 },
    result = {};

['tier1', 'tier2'].forEach(k => ['left', 'right'].forEach(l => {
    var key = [k, l].join('_');
    result[key] = object[key + 'start'] - object[key + 'end'];
}));

console.log(result);