英文:
Function json() returns an Object with an array inside, I want an array of objects
问题
我正在使用Fetch从JSON服务器获取数据,并将其保存在connection
常量中。在获取数据后,我使用connection.json()
将JSON转换为JavaScript对象。但我无法对这个对象进行映射,因为它是一个内部包含数组的对象。我想将它转换成一个对象数组。我应该怎么做?
useEffect(() => {
list();
}, []);
async function list(){
try{
const connection = await fetch("https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db");
const list = await connection.json();
setItems(list);
console.log(items); // 返回一个内部包含数组的对象
}catch(error){
console.log(error);
}
}
英文:
I'm using Fetch to get data from a json server and save it in the connection
constant. After getting the data, I use connection.json()
to transform the json into a javascript object. But I can't map this object, because it is an Object with an array inside. I would like to turn it into an array of objects. How should I proceed?
useEffect(() => {
list();
}, [ ]);
async function list(){
try{
const connection = await fetch("https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db");
const list = await connection.json();
setItems(list);
console.log(items); // Returns an Object with an array inside
}catch(error){
console.log(error);
}
}
答案1
得分: 0
响应包含节点"lista"。如果直接访问它,您将获得所需的结果。
async function list(){
try{
const connection = await fetch("https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db");
const list = await connection.json();
setItems(list.lista); // 直接访问'lista'属性
console.log(items);
}catch(error){
console.log(error);
}
}
英文:
the response contains the node lista. If you access it directly, you will get the desired result.
async function list(){
try{
const connection = await fetch("https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db");
const list = await connection.json();
setItems(list.lista); // Access the 'lista' property directly
console.log(items);
}catch(error){
console.log(error);
}
}
答案2
得分: -1
if you want to render the array of objects,
you can use something like this,
const [items, setItems] = useState([]);
async function list() {
try {
const connection = await fetch(
'https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db'
);
const list = await connection.json();
setItems(list.lista);
console.log(list.lista); // used this to get access of the array
} catch (error) {
console.log(error);
}
}
return (
{items.map((i) => (
<span>
<div>{i.id}</div>
<div>{i.nome}</div>
</span>
))}
)
英文:
if you want to render the array of objects,
you can use something like this,
const [items, setItems] = useState([]);
async function list() {
try {
const connection = await fetch(
'https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db'
);
const list = await connection.json();
setItems(list.lista);
console.log(list.lista); // used this to get access of the array
} catch (error) {
console.log(error);
}
}
return (
{item.map((i) => (
<span>
<div>{i.id}</div>
<div>{i.nome}</div>
</span>
))}
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论