简体   繁体   中英

React: How to store an array of objects inside an array from a JSON file?

I have a JSON file that contains an array of objects, for example:

[
    {"name": "Timothy", "age": 14},
    {....},
    {....},
    ...
]

I want to create an array, eg people , and store basically the exact same information contained in the JSON file. In other words, I want to store that array itself inside the people array I just declared. How could I do this?

In your case try to use spread operator:

import people from './people.json';

const myPeople = [
    {"name": "Timothy", "age": 14}, 
    {...},
    {...}
];
people.people = [...myPeople];

people.json file

{
    "name": "Test",
    "people": []
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM