- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2024 06:13 PM
Hi All,
Please suggest me which is best practice
var newcats=[];
var cat=['cat1','cat2','cat3','cat4','cat5','cat6'];
gs.info(typeof cat); //Output: object
if(cat.toString().indexOf(gr.u_cat.toString())==-1){
newcats.push(gr.u_cat.toString());
}
OR
var newcats=[];
var cat=['cat1','cat2','cat3','cat4','cat5','cat6'];
gs.info(typeof cat); //Output: object
if(cat.indexOf(gr.u_cat.toString())==-1){
newcats.push(gr.u_cat.toString());
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2024 06:20 PM
Hello @Supriya25
Method 2 is more preferable. We don't need to convert array data into string as it already in string format.
If data is not in string format then go for method 1.
If this helped please hit like and mark it as an accepted solution
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2024 06:20 PM
Hello @Supriya25
Method 2 is more preferable. We don't need to convert array data into string as it already in string format.
If data is not in string format then go for method 1.
If this helped please hit like and mark it as an accepted solution
Thank You
Juhi Poddar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 06:36 AM
Thank Juhi.!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2024 06:22 PM
Hi @Supriya25 ,
2nd approach is good, getting the value using indexOf from arrays is good way.
if you are using .toString() then it will convert the array in comma separated value and then perform the index of which is not required.
Accept the solution if it helped
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-22-2024 06:39 AM
Thanks Runjay.! I do follow your YouTube channel .