Javascript Object.assign(REACT)输出不正确且未按预期工作

Javascript Object.assign(REACT)输出不正确且未按预期工作,javascript,json,reactjs,react-native,Javascript,Json,Reactjs,React Native,伙计们,我有一个关于react的项目,有3个组件,我必须了解这3个页面的状态,并在最后一个页面中装载一个JSON 我的组件不是子组件和父组件,所以我找到的解决方案是将状态设置为Localstorage,获取状态并转换为Json,它起作用了,我有我的Json,但我需要将这些Json组合成一个, 我尝试了concat,但它不起作用,我正在尝试Object.assign。我看到了一些示例和文档,但它对我不起作用,我不明白为什么 JSON 1: { "A1_EMAIL": "xxxxx@gmail.co

伙计们,我有一个关于react的项目,有3个组件,我必须了解这3个页面的状态,并在最后一个页面中装载一个JSON

我的组件不是子组件和父组件,所以我找到的解决方案是将状态设置为Localstorage,获取状态并转换为Json,它起作用了,我有我的Json,但我需要将这些Json组合成一个, 我尝试了concat,但它不起作用,我正在尝试Object.assign。我看到了一些示例和文档,但它对我不起作用,我不明白为什么

JSON 1:

{
"A1_EMAIL": "xxxxx@gmail.com",
"formErrors": {
    "A1_EMAIL": "",
    "A1_CGC": ""
},
"A1_EMAILVALID": ["xxxxxx@gmail.com", "xxx", "wwwww.", "com"],
"A1_CGCVALID": false,
"showError": false,
"showErrorCGC": false,
"A1_CGC": "328114xxx14000197",
"A1_NOME": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"A1_NREDUZ": "sssaaaaa",
"A1_SITUA": "ATIVA",
"A1_DTNASC": "19/02/2019",
"NaturezaJ": "213-5 - Empresário (Individual)",
"AtividadePrincipal": "62.01-5-01",
"AtividadeSecundária": "Suporte técnico, manutenção e outros serviços em tecnologia da informação",
"A1_CEP": "04.550-003",
"A1_BAIRRO": "Vila Olímpia",
"A1_EST": "SP",
"A1_END": "xxxxx",
"A1_MUN": "São Paulo",
"checked": true,
"A1_ENDCOB": "gwwwwwww",
"A1_CEPC": "04.550-003",
"A1_BAIRROC": "Vila Olímpia",
"A1_MUNC": "São Paulo"
}

JSON 2:

{
"U5_CONTAT": "e3e3",
"U5_EMAIL": "e3e3",
"A1_INSCR": "fr",
"U5_FONE": "rr"
}

我得到的输出:

{0: "{", 1: """, 2: "U", 3: "5", 4: "_", 5: "C", 6: "O", 7: "N", 8: "T", 9: "A", 10: "T", 11: """, 12: ":", 13: """, 14: "e", 15: "3", 16: "e", 17: "3", 18: """, 19: ",", 20: """, 21: "U", 22: "5", 23: "_", 24: "E", 25: "M", 26: "A", 27: "I", 28: "L", 29: """, 30: ":", 31: """, 32: "e", 33: "3", 34: "e", 35: "3", 36: """, 37: ",", 38: """, 39: "A", 40: "1", 41: "_", 42: "I", 43: "N", 44: "S", 45: "C", 46: "R", 47: """, 48: ":", 49: """, 50: "f", 51: "r", 52: """, 53: ",", 54: """, 55: "U", 56: "5", 57: "_", 58: "F", 59: "O", 60: "N", 61: "E", 62: """, 63: ":", 64: """, 65: "r", 66: "r", 67: """, 68: "}", 69: ":", 70: """, 71: """, 72: "}", 73: ",", 74: """, 75: "A", 76: "1", 77: "_", 78: "E", 79: "M", 80: "A", 81: "I", 82: "L", 83: "V", 84: "A", 85: "L", 86: "I", 87: "D", 88: """, 89: ":", 90: "[", 91: """, 92: "g", 93: "r", 94: "u", 95: "p", 96: "o", 97: "h", 98: "p", 99: "s", …}
我的代码:

var JSON1 = localStorage.getItem('JSONCLIENTE 1')
var JSON2 = localStorage.getItem('JSONCLIENTE 2')
var newObj = Object.assign({}, JSON1,JSON2)

        console.log('Json 1:')
        console.log(JSON1)
        console.log('Json 2:')
        console.log(JSON2)
        console.log('Converted Object 2:')
        console.log(newObj)

我做错了什么

我假设localStorage数据始终是一个字符串,因此在从localStorage检索数据时,您可能也需要使用JSON.parse

如果两个JSON中的键不相同,可以尝试以下操作

变量a={ 留言:“你好” }; 变量b={ 文字:“嗨” }; 变量c={ A. B }; console.logc 因为它是一个字符串。您需要将json转换为js对象。使用JSON.parse

getItem返回字符串,而不是对象。
var JSON1 = JSON.parse(localStorage.getItem('JSONCLIENTE 1'))
var JSON2 = JSON.parse(localStorage.getItem('JSONCLIENTE 2'))
var newObj = Object.assign({}, JSON1,JSON2)

    console.log('Json 1:')
    console.log(JSON1)
    console.log('Json 2:')
    console.log(JSON2)
    console.log('Converted Object 2:')
    console.log(newObj)