0

I have an array with objects including different coordinates such as:

bottomRow: 130.8201356
leftCol: 52.33386899
rightCol: 279.71340993
topRow: 48.9834268

what I need is to render squares based on this data. As many squares as there is different objects in the array.

I have tried this code with no success:

const FaceRecognition = ({imageUrl, box}) => {
     console.log('recog',box)
  return (
    <div className='item-e center ma'>
        <div className='absolute mt2 z-2'>
            <img className='image br4 ma4 z-2' id='inputImage' src={imageUrl} alt=''/>
                {   box.forEach(() => {
                    return (
                        <div className='bounding-box' style={{top: box.topRow, right: box.rightCol, bottom: box.bottomRow, left: box.leftCol}}>
                        </div>
                    )
            }
                
                )
            }
        </div>
    </div>
  );

}

how should I solve this so it takes all the objects and renders all the squares? So far I had to put [0] or any other array index next to box and it renders only that one object. but I need rendering all at once.

update:

I have tried box.map like this:

const FaceRecognition = ({imageUrl, box}) => {
     console.log('recog',box)
  return (
    <div className='item-e center ma'>
        <div className='absolute mt2 z-2'>
            <img className='image br4 ma4 z-2' id='inputImage' src={imageUrl} alt=''/>
                { box.map(((topRow, rightCol, leftCol, bottomRow), idx) => {
                        <div key={idx} className='bounding-box' style={{top: box.topRow, right: box.rightCol, bottom: box.bottomRow, left: box.leftCol}}>
                        </div>
                        }
                    )
                }
        </div>
    </div>
  );
}

still no success, I get Invalid parenthesized assignment pattern now but can not figure out why

3

3 Answers 3

2

First, you should be using map, since forEach will return undefined. Second, you forgot to use the control variable inside map/forEach, you map sould be done like so:

const FaceRecognition = ({imageUrl, box}) => {
     console.log('recog',box)
  return (
    <div className='item-e center ma'>
        <div className='absolute mt2 z-2'>
            <img className='image br4 ma4 z-2' id='inputImage' src={imageUrl} alt=''/>
                {   box.map((element) => {
                    return (
                        <div className='bounding-box' style={{top: element.topRow, right: element.rightCol, bottom: element.bottomRow, left: element.leftCol}}>
                        </div>
                    )
            }
                
                )
            }
        </div>
    </div>
  );

}

The box you are using there is the full array, so it´s impossible to get this elements.

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

1 Comment

the same code that is shared before but well-explained :), upvoted for you
1

You should try the map function and add an arg in the map function.

const FaceRecognition = ({ imageUrl, box }) => {
  return (
    <div className='item-e center ma'>
      <div className='absolute mt2 z-2'>
        <img className='image br4 ma4 z-2' id='inputImage' src={imageUrl} alt='' />
        {box.map((item) => {
          return (
            <div className='bounding-box' style={{ top: item.topRow, right: item.rightCol, bottom: item.bottomRow, left: item.leftCol }}>
            </div>
          )
        })
        }
      </div>
    </div>
  );
}

Comments

0
const FaceRecognition = ({imageUrl, box}) => {
    console.log('recog',box)
    return (
        <div className='item-e center ma'>
            <div className='absolute mt2 z-2'>
                <img className='image br4 ma4 z-2' id='inputImage' src={imageUrl} alt=''/>
                {box.map(({topRow, rightCol, leftCol, bottomRow}, idx) => (
                    <div
                       key={idx}
                       className='bounding-box'
                       style={{
                           top: topRow, 
                           right: rightCol, 
                           bottom: bottomRow, 
                           left: leftCol
                       }}>
                    </div>
                ))}
            </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.