在React Native中将新的键/值添加到对象

时间:2018-09-27 13:15:42

标签: javascript reactjs object react-native add

我正在做一个React Native项目。现在,我正在对象内添加新的键/值。

它正在工作,但是我想知道是否有更好的方法或者您有什么建议。

我还是ReactJS / React Native的新手,而不是Java技能的100%技能。所以这是我的代码:

我的对象

state = {
    result : {
        "q1":1
    }
}

我的功能以添加键/值并修改result的状态:

_getValue = (id, value) => {

    var newObj = this.state.result;
        newObj[id] = parseInt(value);

    this.setState({
        result: newObj
    }, () => {
        console.log(this.state.result)
    })
}

谢谢!

1 个答案:

答案 0 :(得分:2)

这应该工作正常。

 for (int mainLoop = 0; mainLoop < 50; mainLoop++){

        try {
            BufferedWriter writer = writer = new BufferedWriter(new 
FileWriter
                    ("path to file in computer" + mainLoop + ".txt", true));


        for(int forloop = 0; forloop < 50; forloop++) {
            final Document pageHtml= 
Jsoup.connect("link to a page").get();


            Elements body = pageHtml.select("p");

            writer.append(System.getProperty("line.separator"));
            writer.append(System.getProperty("line.separator"));
            writer.append(body.text());

            System.out.println(forloop);
        }
        writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }continue;
    }

它使用了现代/新功能,例如对象传播(this.setState({ result: { ...this.state.result, [id]: value } }); )和动态对象属性(...this.state.result