admin管理员组文章数量:1394154
Currently I have some data in the format of
data : {
full_name: 'John Doe',
date_of_birth: '01-01-1990'
}
I want to render it in a table format which would sort of look like
Full Name : John Doe
Date Of Birth: 01-01-1990
If it was an array, I could just do something like
data.map((item) => <h1>item</h1>
but I also need the to print the key.
How do I achieve something like this in a single loop like function? I could have used map in case of an array, but I also need to render the key part of the object.
NOTE: Data object cannot be changed.
Currently I have some data in the format of
data : {
full_name: 'John Doe',
date_of_birth: '01-01-1990'
}
I want to render it in a table format which would sort of look like
Full Name : John Doe
Date Of Birth: 01-01-1990
If it was an array, I could just do something like
data.map((item) => <h1>item</h1>
but I also need the to print the key.
How do I achieve something like this in a single loop like function? I could have used map in case of an array, but I also need to render the key part of the object.
NOTE: Data object cannot be changed.
Share Improve this question edited Nov 14, 2021 at 9:39 Jeff Schaller 2,6008 gold badges26 silver badges42 bronze badges asked Nov 2, 2021 at 11:30 user16516348user16516348 1179 bronze badges 4-
Maybe something like
<td>{data.full_name}</td>
? – evolutionxbox Commented Nov 2, 2021 at 11:32 - This might be helpful for you reactjs/docs/rendering-elements.html – rajabraza Commented Nov 2, 2021 at 11:33
- @evolutionxbox this was just an example so i put less data, there is a lot of data in the actual object so i need something like map instead AND i also need to render the keys. – user16516348 Commented Nov 2, 2021 at 11:35
-
2
You can use for to loop and have access to the key and value
for (const [key, value] of Object.entries(object1)) { console.log(`${key}: ${value}`); }
inside this loop you can parse your key to replace _ with space and Capitalize First Character after space – Gabriel Commented Nov 2, 2021 at 11:37
5 Answers
Reset to default 3You can map the entries array, like this:
Object.entries(data).map((entry, index) => (
<div key={index}>{entry[0]}: {entry[1]}</div>
));
the Object.entries
function creates an array of [key, value]
arrays from the entries of the function.
Also, you may use a function that will convert keys from snake_case
to readable format. Here is a one-line function that does this conversion:
const snakeToText = (str) => str.split('_').map((word) => word[0].toUpperCase() + word.substring(1)).join(' ');
Let say you have an object
const obj = {
data: {
full_name: "John Doe",
date_of_birth: "01-01-1990"
}
};
then you can use either of the following ways:
Live Demo
1) Using Object.keys
<div>
{Object.keys(obj.data).map((key) => {
return (
<div> {key} - {obj.data[key]} </div>
);
})}
</div>
2) Using Object.entries
<div>
{Object.entries(obj.data).map(([key, value]) => {
return (
<div>
{key} - {value}
</div>
);
})}
</div>
EDITED: If you want to convert my_name
to My Name
then you can create a utility function. You can use split
, map
, and join
.
function convertString(str) {
return str
.split("_")
.map((str) => str[0].toUpperCase() + str.slice(1))
.join(" ");
}
and call this function as:
<div>
{key.includes("_") ? convertString(key) : key} - {obj.data[key]}
</div>
You need to convert your data in Object.entries
or Object.values
.
Object.entries() and Object.values()
Example :
Object.entries()
const object1 = {
a: 'somestring',
b: 42
};
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
// expected output:
// "a: somestring"
// "b: 42"
Object.values()
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.values(object1));
// expected output: Array ["somestring", 42, false]
const data = {
full_name: 'John Doe',
date_of_birth: '01-01-1990'
};
const values = Object.entries(data);
console.log(values)
values.map(entry => console.log(entry[0] + " " + entry[1]));
You can do this like ,
let data = {
full_name: 'John Doe',
date_of_birth: '01-01-1990'
}
Object.keys(data).map((eachKey)=>{
return `${eachKey} : ${data[eachKey]}`
})
const data = {
full_name: "John Doe",
date_of_birth: "01-01-1990"
};
export default function App() {
const renderableData = () =>
Object.keys(data).map((k) => {
const label = k
.replaceAll("_", " ")
.split(" ")
.map((i) => i[0].toUpperCase() + i.slice(1))
.join(" ");
return (
<div>
<div style={{ display: "inline-block" }}>{label}: </div>
<div style={{ display: "inline-block" }}>{data[k]}</div>
</div>
);
});
return <div>{renderableData()}</div>;
}
example here: https://codesandbox.io/s/crazy-hill-hfvl5?file=/src/App.tsx:0-560
本文标签: javascriptHow to map objects into uiStack Overflow
版权声明:本文标题:javascript - How to map objects into ui? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744671657a2618860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论