/    Sign up×
Community /Pin to ProfileBookmark

Simplify Javascript?

This code works as expected, but I looks amateurish to me. Its purpose is to convert at form into a JSON string.

The simple task is to not add the “id” if it is a “new” record.
Like pseudocode: **if it is a new record skip id**

“`
formData.forEach(function(value, key) {
switch (action) {
case “new”:
if (key != mod_id) {
object[key] = value;
}
break;
default:
object[key] = value;
}
});
“`

I wonder if it could be simplified without losing readability?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@sibertauthorFeb 22.2022 — ["Simplify Javascript","Simplify Javascript?"]
Copy linkTweet thisAlerts:
Image@NogDogFeb 22.2022 — As a non-JavaScript person, my first thought is:
[code=javascript]
formData.forEach(function(value, key) {
if(action != "new" || key == mod_id) {
object[key] = value
}
}
[/code]


Copy linkTweet thisAlerts:
@sibertauthorFeb 22.2022 — > @NogDog#1642769

> if(action != "new" || key == mod_id) {

>      object[key] = value

> }


It is exactly this simple code I am looking for, but sorry, this result in a JSON with only the key id {"tsk_id":""}

It should be the opposite.
Copy linkTweet thisAlerts:
Image@NogDogFeb 22.2022 — Ooh, think I got my condensed logic confused. How about...
[code=javascript]
if(action != "new" || key != mod_id)
[/code]
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 1.24,
social: @webDeveloperHQ,
});

legal: ({
terms: of use,
privacy: policy
analytics: Fullres
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @dert,
tipped: article
amount: 1000 SATS,

tipper: @viney352,
tipped: article
amount: 10 SATS,
)...