如何将数据保存回原始json文件?

问题描述 投票:0回答:2
    state = {one_book : []}


updateName = event =>{
        var newvalue = event.target.value
        const id = 1;
        let one_book = this.state.one_book;     // create the copy of state array
        one_book[1] = newvalue;                  //new value
        this.setState({ one_book }); 
        console.log(this.state.one_book)
    }

这里新值是我们要更改为json文件的更新值。我想用这个新名称或用one_book更新json文件中整个对象的ID = 1的书名。更改应反映在原始json文件中]

I have to update this state into json file with the respective id.
Here is my json file
[
    {
            "id": 1,
            "bookname": "Physics",
            "price": 600,
            "author": "ABC",
            "pages": 567,
            "category" : "School Books"
    },
    {
            "id": 2,
            "bookname": "Let Us C",
            "price": 1300,
            "author": "XYZ",
            "pages": 1267,
            "category" : "Technical"  
    }
]

状态= {one_book:[]} updateName =事件=> {var newvalue = event.target.value const id = 1;让one_book = this.state.one_book; //创建状态数组的副本...

json reactjs state form-data insert-update
2个回答
0
投票

let one_book = this.state.one_book;不会创建数组的副本。 Javascript数组和对象作为引用存储在变量中。


0
投票

updateName =事件=> {var newvalue = event.target.valueconst id = 1;

© www.soinside.com 2019 - 2024. All rights reserved.