Home > front end >  Merge elements in json and calculate
Merge elements in json and calculate

Time:07-23

I have 1 employee table, employee error after I query and add to array js like this:

[
{ id: 01,
  name: a,
  error: "late",
  counterorr: 3 
}, 
{ id:01,
  name: a, 
  error: "forgot homework",
  counterorr:2 
},
{ id: 02,
  name: b,
  error: "late",
  counterorr:2
}
]

I'm not very good with json and handling them so I don't understand how to best store it What I want to display on the screen:

{id:01, name: a,error:[{nameErorr: late,counterorr:3}, {nameErorr:forgot homework,counterorr:3
}], id: 02, name: b, error:[{nameErorr:"late",counterorr:2}]}

I can't find a better way to store it, if so please help me, thanks

I will describe it more simply than this

user=[{
id: 01,
have: orange
number: 1
},{
id: 01,
have: banana
number: 2
},{
id: 02,
have: orange
number: 1
}]

i want to convert that array to like this

user=[{
id: 01,
have: [{name: orange, number: 1}, {name: banana, number: 2}]
},{
id: 02,
have: [{name: orange, number: 1}]
}]

is this possible using lodash _groupby?

CodePudding user response:

try use array for employee table and errors property

[
  {
    id: 01,
    name: a,
    errors: [
      { nameErorr: late, counterorr: 3 },
      { nameErorr: forgot, homework, counterorr: 3 },
    ],
  },
  { id: 02, name: b, errors: [{ nameErorr: "late", counterorr: 2 }] },
];

CodePudding user response:

You need to process the arrays based on the content. See if the item already exists in the processed array, if so, modify it. If not, add a new entry.

.reduce would be handy in this case.

For the first table.

const employeeError = [
  {
    id: 01,
    name: "a",
    error: "late",
    counterorr: 3
  },
  {
    id: 01,
    name: "a",
    error: "forgot homework",
    counterorr: 2
  },
  {
    id: 02,
    name: "b",
    error: "late",
    counterorr: 2
  }
]

const processedEmployeeError = employeeError.reduce((acc, cur) => {
  const existingEployee = acc.find(emp => emp.id === cur.id);

  if(existingEployee) {
    existingEployee.error.push({nameError: cur.error, counterorr: cur.counterorr})
  } else {
    acc.push({
      id: cur.id,
      name: cur.name,
      error: [{
        nameError: cur.error,
        counterror: cur.counterorr
      }]
    });
  }

  return acc;
  
},[]);

console.log(processedEmployeeError);

/*
[{
    "id": 1,
    "name": "a",
    "error": [{
        "nameError": "late",
        "counterror": 3
    }, {
        "nameError": "forgot homework",
        "counterorr": 2
    }]
}, {
    "id": 2,
    "name": "b",
    "error": [{
        "nameError": "late",
        "counterror": 2
    }]
}]
*/

Similarly, for other employee table.

const user = [{
  id: 01,
  have: 'orange',
  number: 1
}, {
  id: 01,
  have: 'banana',
  number: 2
}, {
  id: 02,
  have: 'orange',
  number: 1
}];

const processedUser = user.reduce((acc, cur) => {
  const existingUser = acc.find(u => u.id === cur.id);

  if(existingUser) {
    const existingFruits = existingUser.have.find(f => f.name === cur.have);
    if(existingFruits) {
      existingFruits.number  = cur.number;
    }
  } else {
    acc.push({
      id: cur.id,
      have: [{
        name: cur.have,
        number: cur.number
      }]
    });
  }
  return acc;
}, []);

console.log(processedUser);
  • Related