i have an object data like this in my javascript code. When i have keys with filled values like title_en and description_en, i want to copy of its content to title_fr and description_fr.
This is my data
{
"id": "2331",
"title_en" : "Something great"
"title_fr": "",
"description_en" : "Lorem ipsum something etc."
"description_fr": "",
"tag_en": "im filled",
"tag_fr": "another filled",
}
this is how it should be
{
"id": "2331",
"title_en" : "Something great"
"title_fr": "Something great",
"description_en" : "Lorem ipsum something etc."
"description_fr": "Lorem ipsum something etc.",
"tag_en": "im filled",
"tag_fr": "another filled",
}
how can i accomplish this with jquery or js?
2
Answers
This should replace any
_fr
with its_en
counterpart. I would personally check not just equality to""
, but also possibleundefined
. But it depends on your data structure.After spending a good time to work on this requirement, Here I am with the solution. Please give a try to this solution. It will work for multiple substrings separated by underscore as well.
Live Demo (Descriptive comments has been added into the below code snippet) :