Link to home
Start Free TrialLog in
Avatar of coder
coderFlag for Australia

asked on

Iterate through reactjs function for 5 times

Hi experts,

I want to iterate reactjs function for object (metadata) for 5 times

The javascript object values are in the below console.log image

User generated image
Please see the code I have

 console.log(metaKeyValues, '-  metadata ');

    var listMetadata = Object.entries(metaKeyValues).map(([key,value])=>{
         return (
           <div style={{display:"block",width:800}}>
                <div className="row">
                     <span style={{display:'inline-block',minWidth:100,padding:5}}> {key} </span>
                     <span style={{display:'inline-block',minWidth:400,padding:10}}> {value} </span>
                </div>
           </div>
         )});

Open in new window


I want to iterate this, for 5 times.  Something like this,  but it is not working.

 var index = 0;
 var testListMetadata = Object.entries(metaKeyValues).map((([key,value]),index)=>{
         return(
             if(index < 5) {
             <div style={{display:"block",width:800}}>
                  <div className="row">
                       <span style={{display:'inline-block',minWidth:100,padding:5}}> {key} </span>
                       <span style={{display:'inline-block',minWidth:400,padding:10}}> {value} </span>
                  </div>
             </div>
             index ++;
          })
    });

Open in new window


Please help me in resolving this issue.

with Many thanks,

Bharath AK
Avatar of Dorababu M
Dorababu M
Flag of India image

How may objects you are getting in
Object.entries

Open in new window

I am not sure if your object is correct then you can do as follows

var size = 3;
var items = list.slice(0, size).map(i => {
    return <myview item={i} key={i.id} />
}

return (
  <div>
    {items}
  </div>   
)

Open in new window

Avatar of coder

ASKER

Hi Dorababu,

Thanks for your response.

I had tried the following code

it throws an error "Uncaught (in promise) TypeError: metaKeyValues.slice is not a function"

I have the following code

 var metaKeyValues={ };

    var i,key;
    for(i = 0;i < metaObject.length;i++){
       if((i%2) == 0) {
           key = metaObject[i];
       }
       else{
           metaKeyValues[key] = metaObject[i];
       }
    }

var listMetadata = Object.entries(metaKeyValues).map(([key,value])=>{
         return (
           <div style={{display:"block",width:800}}>
                <div className="row">
                     <span style={{display:'inline-block',minWidth:100,padding:5}}> {key} </span>
                     <span style={{display:'inline-block',minWidth:400,padding:10}}> {value} </span>
                </div>
           </div>
         )});


     var testListMetadata = metaKeyValues.slice(0,5).map(([key,value])=>{
         return (
           <div style={{display:"block",width:800}}>
                <div className="row">
                     <span style={{display:'inline-block',minWidth:100,padding:5}}> {key} </span>
                     <span style={{display:'inline-block',minWidth:400,padding:10}}> {value} </span>
                </div>
           </div>
        )
    });

Open in new window


the error occurs even when I change the code

  var metaKeyValues = [];

Open in new window

the error is "Stack trace: TypeError: metaKeyValues.slice is not a function"

and the object has about around 20 key-value pairs.

Is there any other way I can do instead of object?

Please help me in resolving this error.

With May thanks,
Bharath AK
A sample example can you try this and let me know

const numrows = [{"id" : 01, "name" : "abc"}];
 <tbody>
       {numrows.map(data=> {
             return <ObjectRow key={data.id} name={data.name}/>
       })}
</tbody>

Open in new window

How about  
return (<div>
    {metaKeyValues.map((obj, index) => (
if(index<5)
{
        <p>Hello, {obj.name} from {obj.country}!</p>
}
    ))}
    </div>);

Open in new window

Avatar of coder

ASKER

If we don't know keys are generated at runtime. how to get keys and values if keys are generated at runtime.

for example.

if we don't know the keys (name, id, country) and they are generated at runtime.

for eg

var listMetadata = Object.entries(metaKeyValues).map(([key,value])=>{
         return (
           <div style={{display:"block",width:800}}>
                <div className="row">
                     <span style={{display:'inline-block',minWidth:100,padding:5}}> {key} </span>
                     <span style={{display:'inline-block',minWidth:400,padding:10}}> {value} </span>
                </div>
           </div>
         )});

Open in new window


In the above eg, I don't know the key, as well as value, both, are used at runtime. in addition to it, I want to restrict to 5 iterations.

How to achieve this.

Please get back to me.
ASKER CERTIFIED SOLUTION
Avatar of coder
coder
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of coder

ASKER

Thanks Dorababu for your suggestions and help