coder
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
Please see the code I have
I want to iterate this, for 5 times. Something like this, but it is not working.
Please help me in resolving this issue.
with Many thanks,
Bharath AK
I want to iterate reactjs function for object (metadata) for 5 times
The javascript object values are in the below console.log 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>
)});
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 ++;
})
});
Please help me in resolving this issue.
with Many thanks,
Bharath AK
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>
)
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
the error occurs even when I change the code
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
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>
)
});
the error occurs even when I change the code
var metaKeyValues = [];
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>
How about
return (<div>
{metaKeyValues.map((obj, index) => (
if(index<5)
{
<p>Hello, {obj.name} from {obj.country}!</p>
}
))}
</div>);
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
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.
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>
)});
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Dorababu for your suggestions and help
Open in new window