0

I have an array of objects like below.

        var array1 = [
    { col1: 'ABC1+R2', col2: 'ABC', col3: 'R20'},
    { col1: 'ABC1+R3', col2: 'ABC', col3: 'R20'},
    { col1: 'ABC1+R2', col2: 'ABC', col3: 'R301'},
    { col1: 'ABC1+R3', col2: 'ABC', col3: 'R301'},
    { col1: 'CDE2+R4', col2: 'CDE', col3: 'R20'},
    { col1: 'CDE2+R5', col2: 'CDE', col3: 'R30'},
    { col1: 'RED4+R3', col2: 'RED', col3: 'D20'},
    { col1: 'GTR5+R2', col2: 'GTR', col3: 'R20'}];
    
    var result = array1.reduce(function(r, a) {
                r[a.col2] = r[a.col2] || [];
                r[a.col2].push(a);
                return r;
              }, Object.create(null));
              
console.log(result);

I tried the above script, but couldn't achieve. somewhere i did mistake.

i want the output like below need to group col1 and col3 based on col2. Thanks in advance.

  var res = [  
    {
      "col1": [{text1: "ABC1+R2"},{text1: "ABC1+R"}],
      "col2": "ABC",
      "col3": [{text2: "R20"},{text2: "R301"}],
    }, 
    {
      "col1": [{text1: "CDE2+R4"},{text1: "CDE2+R5"}],
      "col2": "CDE",
      "col3": [{text2: "R20"},{text2: "R30"}],
    }, 
    {
      "col1": "RED4+R5",
      "col2": "RED",
      "col3": "D20"
    },
 
    {
      "col1": "GRT3",
      "col2": "ED",
      "col3": "R20"
    }
  ];
  
  console.log(res);

1
  • The output is not consistent? Would you consider to change col1 and col2 to array all the time? Commented Jan 17, 2022 at 11:56

3 Answers 3

2

Try this one

var array1 = [
  { col1: 'ABC1+R2', col2: 'ABC', col3: 'R20'},
  { col1: 'ABC1+R3', col2: 'ABC', col3: 'R20'},
  { col1: 'ABC1+R2', col2: 'ABC', col3: 'R301'},
  { col1: 'ABC1+R3', col2: 'ABC', col3: 'R301'},
  { col1: 'CDE2+R4', col2: 'CDE', col3: 'R20'},
  { col1: 'CDE2+R5', col2: 'CDE', col3: 'R30'},
  { col1: 'RED4+R3', col2: 'RED', col3: 'D20'},
  { col1: 'GTR5+R2', col2: 'GTR', col3: 'R20'}];
  
var result = array1.reduce(function(r, a) {
  let obj = r.find(el => el.col2 === a.col2);
  const text1 = {"text1": a.col1};
  const text2 = {"text2": a.col3};
  if(obj) {
    if(!obj.col1.find(el => el.text1 === text1.text1)) {
      obj.col1.push(text1);
    }
    if(!obj.col3.find(el => el.text2 === text2.text2)) {
      obj.col3.push(text2);
    }
  } else {
    obj = {
      col1: [text1],
      col2: a.col2,
      col3: [text2]
    }
    r.push(obj)
  }
  return r;
}, []);

console.log(result);

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

Comments

1

You could either chgeck the object if the key exist, then add a single object otherwise check if one of the properties is not an array as value, then convert the plain value to an object inside of an array.

const
    data = [{ col1: 'ABC1+R2', col2: 'ABC', col3: 'R20'}, { col1: 'ABC1+R3', col2: 'ABC', col3: 'R20'}, { col1: 'ABC1+R2', col2: 'ABC', col3: 'R301'}, { col1: 'ABC1+R3', col2: 'ABC', col3: 'R301'}, { col1: 'CDE2+R4', col2: 'CDE', col3: 'R20'}, { col1: 'CDE2+R5', col2: 'CDE', col3: 'R30'}, { col1: 'RED4+R3', col2: 'RED', col3: 'D20'}, { col1: 'GTR5+R2', col2: 'GTR', col3: 'R20'}],
    result = Object.values(data.reduce(function(r, { col1, col2, col3 }) {
        if (r[col2]) {
            if (!Array.isArray(r[col2].col1)) {
                r[col2].col1 = [{ text1: r[col2].col1 }];
                r[col2].col3 = [{ text2: r[col2].col3 }];
            }
            r[col2].col1.push({ text1: col1 });
            r[col2].col3.push({ text2: col3 });
        } else {
            r[col2] = { col1, col2, col3 };
        }
        return r;
    }, Object.create(null)));

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

Comments

0
var array1 = [
{ col1: 'ABC1+R2', col2: 'ABC', col3: 'R20'},
{ col1: 'ABC1+R3', col2: 'ABC', col3: 'R20'},
{ col1: 'ABC1+R2', col2: 'ABC', col3: 'R301'},
{ col1: 'ABC1+R3', col2: 'ABC', col3: 'R301'},
{ col1: 'CDE2+R4', col2: 'CDE', col3: 'R20'},
{ col1: 'CDE2+R5', col2: 'CDE', col3: 'R30'},
{ col1: 'RED4+R3', col2: 'RED', col3: 'D20'},
{ col1: 'GTR5+R2', col2: 'GTR', col3: 'R20'}];

var result = array1.reduce(function(r, a) {
            r[a.col2] = r[a.col2] || [];
            r[a.col2].push(a);
            return r;
          }, Object.create(null));
let arr = []

for(let k in result){
  let col1 = result[k].length > 1 ? result[k].map(el => ({text: el.col1})) : result[k][0]['col1'];
  let col2 = k;
  let col3 = result[k].length > 1 ? result[k].map(el => ({text: el.col3})) : result[k][0]['col1'];
  arr.push({col1, col2, col3})
 }

 console.log(arr);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.