1.不同的JSON格式的数据的字符串进行拼接,借助forin
let dataOne = "{\"name\":\"brant\"}"
let dataTwo = "{\"name\": \"Jane\",\"age\": \"22\"}"
let dataOneJSON = JSON.parse(dataOne )
let dataTwoNew = dataTwo.substr(0,dataTwo.length()-1)
for (const key in dataOneJSON )
{
dataTwoNew += ",\""+key+"\":\""+dataOneJSON[key]+"\""
}
dataTwoNew += "}"
console.log(dataTwoNew)
2.JSON数据删除包含的某个JSON相同的数据
let dataOne = "{\"name\":\"brant\"}"
let dataTwo = "{\"name\": \"brant\",\"age\": \"22\"}"
let dataOneJSON = JSON.parse(dataOne )
let dataTwoJSON = JSON.parse(dataTwo )
for (const keyTwo in dataTwoJSON )
{
for (const keyOne in dataOneJSON )
{
if(keyTwo == keyOne && dataOneJSON[keyOne] == dataTwoJSON[keyTwo ] ){
dataTwo delete dataTwo[keyTwo+""]
}
}
}
console.log(dataTwoJSON )