user14232549
user14232549

Reputation: 413

How to loop an object in React

data is here subprojects={"RFv6bJMG2dmiBqWNZX8O":{"content":"this is ・・data"}}

console.log("subprojects=" + JSON.stringify(subprojects));
const SubList = ({ subprojects }) => {
if(subprojects){
  return (
    <div className="project-list section">
      {subprojects &&
        subprojects.map((subproject) => {
          return (
            <div>
              {subproject.id}
            </div>
          );
        })}
    </div>
  );}
  else{

The result is TypeError: subprojects.map is not a function

Upvotes: 0

Views: 65

Answers (1)

m5khan
m5khan

Reputation: 2717

use Object.entries. Check MDN Docs.

      {
        Object.entries(subprojects).map(([key, val]) => {
          return (
            <div key={key}>
              {val.id}
            </div>
          );
        })}

Alternatively you can also use Object.values if you are not concerned about the keys

Upvotes: 3

Related Questions