0

This object contains --> array --> object. Using for/foreach or another method I need to iterate the ROW in the table. This is React JSX code. Could someone please help me understand what to do?

a:{
 bb: [{}],
 cc: [{}],
 dd: [{}]
}

I need to iterate object "a" using dynamic "key" and render JSX Row code in a Table using attribute in side the nested objects.

render() {
    const {
      data: {
        drivers: { user, month, year, createdAt },
      },
    } = this.props;

let DriverCommissionResults = {};
let recent = "";
let combinedUser = {};

DriverCommissionResults = {
   "Jane": 200,
   "Ann": 100,
   "Kevin": 50,

}

for (const [key, value] of Object.entries(DriverCommissionResults)) {
        combinedUser[`${key}`] = user.reduce(function (filtered, option) {
          if (option.username === key) {
            var someNewValue = {
              userId: option.userId,
              username: option.username,
              createdAt: option.createdAt,
              ranking: option.ranking,
              year: option.year,
              month: option.month,
              handle: option.handle,
              initialSalary: option.initialSalary,
              salary: option.salary,
              drunkenPesentage: option.drunkenPesentage,
              newTotalCommission: value,
            };
            filtered.push(someNewValue);
          }
          return filtered;
        }, []);
      }

      for (const [key, value] of Object.entries(DriverCommissionResults)) {
        recent = combinedUser[`${key}`].map((filteredPerson) => (
          <RowComponent
            newSalary={filteredPerson.initialSalary}
            key={filteredPerson.userId}
            member={filteredPerson}
            card={cardMode}
            newTotalCommission={filteredPerson.newTotalCommission}
          />
        ));
      }

return (
  <Fragment>
    <table>
      <thead>
        <tr>
          <th>Handle</th>
          <th>InitialSalary</th>
          <th>Salary</th>
        </tr>
      </thead>
      <tbody>{recent}</tbody>
    </table>
  </Fragment>
}


console.log(combinedUser);

{Jane: Array(1), Ann: Array(1), Kevin: Array(1)}

{
Jane: [{handle: "Jane", username: "Jane", salary: 40000, initialSalary: 40100, }],
Ann: [{handle: "Ann", username: "Ann", salary: 50000, initialSalary: 50100,}],
Kevin: [{handle: "Kevin", username: "Kevin", salary: 30000, initialSalary: 30100,}],
}

Output

Handle InitialSalary Salary

Kevin 30000 30100

Expected Output

Handle InitialSalary Salary

Jane 40000 40100

Ann 50000 50100

Kevin 30000 30100

2 Answers 2

1

The variable recent got overwritten inside the for loop. Try this out

const recent = []
for (const [key, value] of Object.entries(DriverCommissionResults)) {
    combinedUser[`${key}`].forEach((filteredPerson) => (
      recent.push(<RowComponent
            newSalary={filteredPerson.initialSalary}
            key={filteredPerson.userId}
            member={filteredPerson}
            card={cardMode}
            newTotalCommission={filteredPerson.newTotalCommission}
         />);
    ));
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you can build items like below and use it in render.

const obj = {
  Jane: [
    { handle: "Jane", username: "Jane", salary: 40000, initialSalary: 40100 },
  ],
  Ann: [
    { handle: "Ann", username: "Ann", salary: 50000, initialSalary: 50100 },
  ],
  Kevin: [
    { handle: "Kevin", username: "Kevin", salary: 30000, initialSalary: 30100 },
  ],
};

const items = Object.values(obj).map(([rowObj]) => {
  return (
    <div key={rowObj.handle}>
      {Object.entries(rowObj)
        .filter(([key]) =>
          ["username", "salary", "initialSalary"].includes(key)
        )
        .map(([key, value]) => (
          <div key={key}> {value} </div>
        ))}
    </div>
  );
});

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.