1

I have an object array like this:

var input = [{ time: '11:01:00', data: 'a,b,c' }, { time: '11:01:11', data: 'a,b,c' }]

After I call:

var output1 = input.map(a => a.data);
console.log(output1);

The result would be:

[
  "a,b,c",
  "a,b,c"
]

Now I want the final output will be:

[
  {data: ["a","a"]},
  {data: ["b","b"]},
  {data: ["c","c"]},
]

I have tried many way but no hope to do this.

3
  • Can input have data like 'a,b,c,d,e' ? Commented May 25, 2021 at 10:04
  • Yes it will be a string with comma Commented May 25, 2021 at 10:10
  • You want to group based on the similarity of the values or based on their position? If the matrix transposition is what you want, then you can accept that answer :) Commented May 25, 2021 at 10:21

4 Answers 4

3

Your operation is essentially a matrix transposition, let's code it as such:

let transpose = matrix => matrix[0].map((_, col) => 
    matrix.map(row => row[col]))

//

var input = [{ time: '...', data: 'a,b,c' }, { time: '...', data: 'x,y,z' }]


let result = transpose(
    input.map(o => o.data.split(','))
).map(data => ({data}))

console.log(result)

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

Comments

2

Use Array.flatMap() with Array.slice() to get an array of strings. Then reduce it to an object, with equal strings grouped together, and convert back to an array using Object.values():

const input = [{ time: '11:01:00', data: 'a,b,c' }, { time: '11:01:11', data: 'a,b,c' }]

const result = Object.values(
  input.flatMap(o => o.data.split(','))
    .reduce((acc, c) => {
      if(!acc[c]) acc[c] = { data: [] }

      acc[c].data.push(c)

      return acc
    }, {})
)

console.log(result)

Comments

0

You could do it like this:

var input = [{ time: '11:01:00', data: 'a,b,c' }, { time: '11:01:11', data: 'a,b,c' }];

var output1 = input.map(a => a.data);

// Let's split the strings in order to have an array ('a', 'b', 'c')
output1 = output1.map( (data) => data.split(',') );

let valueMap = {};

// Loop through all the values
for (let list of output1) {
   for (let element of list) {
      // Use a dictionary to group all the identical values
      if (!valueMap[element]) {
          valueMap[element] = [element];
      }
      else {
          valueMap[element].push(element);
      }
   } 
}

// Get the values from the object and map it with the desired structure
let result =  Object.values(valueMap).map( (lista) => ({ data: lista }) );

console.log(result);

Comments

0

A single liner.

var input = [{ time: '11:01:00', data: 'a,b,c' }, { time: '11:01:11', data: 'a,b,c' }]


var output1 = input.map(a => a.data.split(','))[0].map((_, idx) => {return {data: input.map(a => a.data.split(',')[idx])}});

console.log(output1);

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.