0

I have an array which is something like this:

[
  { KEY1: 190 },
  { KEY1: 10 },
  { KEY2: 50 },
  { KEY2: 20 },
  { KEY3: 155 },
  { KEY3: 20511 }
]

I want to convert this into:

[
  {
    KEY1: 190,
    KEY2: 50,
    KEY3: 155 
  },
  {
    KEY1: 10,
    KEY2: 20,
    KEY3: 20511
  }
]

These keys are dynamic and can be n number of times.

Sean
  • 6,873
  • 4
  • 21
  • 46
Ravi khatri
  • 575
  • 2
  • 5
  • 11
  • Here keys are dynamic so can't use this as an index like res[obj.name]. I already have mentioned that in my question. The number of keys defines the number of Rows. – Ravi khatri Jun 20 '21 at 15:43
  • 2
    I don't know what you are asking as I don't see by which criterion you want to split the array into groups. – Dan Macak Jun 20 '21 at 15:53
  • Hi Dan, If you see keys are the same in both arrays but their values are different. So on the 0 position we have { TK_CEOLTIPTOTSHARESRESER : 190,TK_CEOSHARESOUTSTANDING : 50 , TK_EMPLOYEEOPTIONSGRANTE : 155 } while first position have { TK_CEOLTIPTOTSHARESRESER : 10, TK_CEOSHARESOUTSTANDING : 20, TK_EMPLOYEEOPTIONSGRANTE : 20511} (Keys are same but values are different) The number of rows will increase in case we have another key. I hope I could explain you. – Ravi khatri Jun 20 '21 at 16:01
  • do identical keys always follow each other or are they scattered? – Mister Jojo Jun 20 '21 at 16:22
  • Are these duplicate keys always in equal number? – Mister Jojo Jun 20 '21 at 16:22
  • If you write a question like this, it is a good habit not to copy and paste your real code, but make it easier for us by changing the keys into something less complicated, such as `KEY1`, `KEY2`, `KEY3`. It is not easy to understand what do you want to do with the keys, they are too long. – Honza Zidek Jun 21 '21 at 12:38
  • Ravi, have any of these answers solved your question? If so, please mark one as accepted. – Sean Jul 05 '21 at 22:22

2 Answers2

1

You could take an object for keeping track of the indices of same keys and build objects with only one same key of the given data.

const
    data = [{ TK_CEOLTIPTOTSHARESRESER: 190 }, { TK_CEOLTIPTOTSHARESRESER: 10 }, { TK_CEOSHARESOUTSTANDING: 50 }, { TK_CEOSHARESOUTSTANDING: 20 }, {  TK_EMPLOYEEOPTIONSGRANTE: 155 }, { TK_EMPLOYEEOPTIONSGRANTE: 20511 }],
    indices = {},
    result = data.reduce((r, o) => {
        const
            key = Object.keys(o)[0],
            index = indices[key] = (indices[key] ?? -1) + 1;

        Object.assign(r[index] ??= {}, o);
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Nina, I edited the question so it is easier to read. Please consider updating your answer, I don't want to meddle with it without your consent :) – Honza Zidek Jun 21 '21 at 12:42
  • this answer works for the changing data as well. no need to change something. – Nina Scholz Jun 21 '21 at 12:52
  • Ha! I didn't know you could add styling to the snippet console. – Sean Jun 21 '21 at 13:25
  • @Nina I renamed the keys so they are better understandable. `KEY1`, `KEY2`, `KEY3` is better readable than `TK_CEOLTIPTOTSHARESRESER`, `TK_CEOLTIPTOTSHARESRESER`. I think your answer would be also better readable if you do not stick with the OP's messy keys. – Honza Zidek Jun 22 '21 at 12:33
0

This would be another (more easily understandable, in my opinion) way to approach it. It creates an array of new objects, and assigns the key and value to the earliest object that doesn't yet have that key.

const oldData = [
  { KEY1: 190 },
  { KEY1: 10 },
  { KEY2: 50 },
  { KEY2: 20 },
  { KEY3: 155 },
  { KEY3: 20511 }
]

const convert = (arr) => {
  let newArr = []
  arr.forEach((obj) => {
    for (let i = 0; i < newArr.length + 1; i++) {
      newArr[i] = newArr[i] || {}
      let key = Object.keys(obj)[0]
      if (!newArr[i].hasOwnProperty(key)) { 
        newArr[i][key] = obj[key]
        break
      }
    }
  })
  return newArr
}

const newData = convert(oldData)
console.log(newData)
Sean
  • 6,873
  • 4
  • 21
  • 46