I want to add data from another Json file to another without overwriting the existing one. I just can't get any further, the console always gives me the following:
Console output
Data
string
[
"follow1",
"follow2",
"follow3",
"follow4",
"[\"follow5\",\"follow6\",\"follow7\",\"follow8\",\"follow9\"]"
]
This is my code, I would like to add the data but without square brackets and without backslashes. It would be really nice if someone could help me. Thanks very much
const user = require('./user.json');
const oldUser = user.user_follwos["user1"];
const data = require('./data.json');
const toAdd = JSON.stringify(data);
const result = JSON.stringify(toAdd);
oldUser.push(...toAdd.split(' '))
const newData = JSON.stringify(oldUser, null, 4)
console.log('\nData \n' + typeof newData + ' \n' + newData);
and here are my json files
//user.json
{
"application_id": "123546789",
"user_follwos": {
"user1": [
"follow1",
"follow2",
"follow3",
"follow4"
],
"user2": [
"followA",
"followB",
"followC",
"followD"
]
},
...
...
}
//data.json
[
"follow5",
"follow6",
"follow7",
"follow8",
"follow9"
]
First of all to do something you need both data in json
let allTogether = data.push(...oldUser);
uniq = [...new Set(allTogether )];
user_follwos.user1 = uniq
Hope this is what you need
let user = {
"application_id": "123546789",
"user_follwos": {
"user1": [
"follow1",
"follow2",
"follow3",
"follow4"
],
"user2": [
"followA",
"followB",
"followC",
"followD"
]
}
};
let data = [
"follow5",
"follow6",
"follow7",
"follow8",
"follow9"
];
let oldUser = user["user_follwos"]["user1"];
console.log(`This is old user array`);
console.log(oldUser);
let allTogether = [];
allTogether.push(...data)
allTogether.push(...oldUser);
console.log(`After we put all together`);
console.log(allTogether);
uniq = [...new Set(allTogether )];
console.log(`Getting unique values`);
console.log(uniq);
oldUser = uniq;
console.log(`Now olds user is`);
console.log(oldUser);