0

Hi I am hitting the api endpoint and it return this object

{
  "groupNames": ["terminator", "human"],
  "imgData": {
    "0": {
      "filepath": "0.jpg",
      "group": "terminator",
      "id": "0",
      "isClicked": false,
      "title": "image_0"
    },
    "1": {
      "filepath": "1.jpg",
      "group": "terminator",
      "id": "1",
      "isClicked": false,
      "title": "image_1"
    },
    "2": {
      "filepath": "2.jpg",
      "group": "terminator",
      "id": "2",
      "isClicked": false,
      "title": "image_2"
    }
 }
}

my question is how I can map over imgData object and render every "group" field inside a

tag?

1

1 Answer 1

1

Something like this?

import React from 'react';
import './style.css';

let data = {
  groupNames: ['terminator', 'human'],
  imgData: {
    0: {
      filepath: '0.jpg',
      group: 'terminator',
      id: '0',
      isClicked: false,
      title: 'image_0',
    },
    1: {
      filepath: '1.jpg',
      group: 'terminator',
      id: '1',
      isClicked: false,
      title: 'image_1',
    },
    2: {
      filepath: '2.jpg',
      group: 'terminator',
      id: '2',
      isClicked: false,
      title: 'image_2',
    },
  },
};
export default function App() {
  return (
    <div>
      {Object.values(data.imgData).map((x) => (
        <div>{x.group}</div> // add proper keys to these divs in real code
      ))}
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

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.