Reputation: 168
I'm having quite some trouble handling objects and storing them as JSON.
What I'm trying to do is next:
var links = JSON.parse([{"user1":{"username":"user1","data":"164"}},{"user2":{"username":"user2","status":0}}]);
var key = dater['username']; //dynamic username strings
var ACCOUNT = {
username: key,
data: value,
status:1
}
if(links.hasOwnProperty(key)){
links[key] = ACCOUNT;
links=JSON.stringify(links);
fs.writeFile('status.json', links, err => {
// error checking
if(err) throw err;
console.log('Exists.. updating.');
});
}else{ // ELSE create new entry
links.push({[key]: ACCOUNT});
newData= JSON.stringify(links);
fs.writeFile('status.json', newData, err => {
// error checking
if(err) throw err;
console.log('New user.. creating');
});
}
As you can probably tell, the above doesn't work as I'd want it to. I'm used with PHP arrays where I would simply do arrays with keys and directly update them without any 'if/else'
Upvotes: 0
Views: 1263
Reputation: 1540
First of all, your usage of JSON.parse
is incorrect, because the purpose of it is to take a string in JSON format and parse it to a JS object (or any other entity encoded in it). Your passing to it an array, and because JS has very expected and intuitive behavior, it tries to convert it to a string which end up being [Object object]
, which should result an error.
I assume it's just a typo and you forgot the "" before and after the array.
Second, links
is an array. When you're using hasOwnProperty
on this array, you're asking whether the array itself has this key, and not whether one of its members has.
There are 2 options:
Either
You can loop over the array with the some
function and check whether one of the objects in the array has this key like so:
const isUserAlreadyExists = links.some(userObj => userObj.hasOwnProperty(key))
And use this as your condition
Or
Remodel your data so it's an object with the user names as keys, something like:
{
user1: {username: "user1", data: whatever},
user2: {username: "user2", data: whatever}
...
}
And then your current solution should work. I would suggest the second solution since your data model seems a bit weird, maybe consider different data structure altogether.
To do that, your initial data will have to be an empty object (const links = {}
or wherever you're getting it from)
And then you can just keep the part in your if
clause and it should work
So your code should be something like:
var key = dater['username']; //dynamic username strings
var ACCOUNT = {
username: key,
data: value,
status:1
}
links[key] = ACCOUNT;
links=JSON.stringify(links);
fs.writeFile('status.json', links, err => {
// error checking
if(err) throw err;
console.log('Exists.. updating.');
});
This way you don't have to check if the key
property exists in your object, because if it doesn't it will simply create it and if it does it will overwrite whatever's stored in there
Upvotes: 1
Reputation: 2545
const key = dater['username']; //dynamic username strings
const account = {
username: key,
data: value,
status: 1,
};
const matchedItem = links.find((i) => i[key]);
if (matchedItem) {
matchedItem[key] = account;
} else {
links.push({ [key]: account });
}
console.log('links:::', links);
fs.writeFile('status.json', JSON.stringify(links), (err) => {
if (err) throw err;
console.log('New user.. creating');
});
Upvotes: 1