If element exists in an array then remove it from the array

Ankita9793
Tera Contributor

Hi All

 

My requirement is, if '5b806c328396c6105fc86770deaad354' is present in 'entitlements' then remove it from 'entitlements'. Can someone please suggest what is wrong with the code.

 

var entitlements = ['0678191f1b13a4d06c3c2f007e4bcb9b','5b806c328396c6105fc86770deaad354'];
var mdmadldsgrp = '5b806c328396c6105fc86770deaad354';
var ent_list = entitlements.split(',');
var cat_item = 'Mobile - NEW';

if (cat_item == "Mobile - NEW"); {

    gs.print('entitlements1 indexOf' + entitlements.indexOf(mdmadldsgrp) +ent_list);

    for (var i = 0; i <= entitlements.length; i++) {
             
    if (entitlements.indexOf(mdmadldsgrp) >= -1) {

        entitlements.splice(mdmadldsgrp);
        // ent_list.splice(ent_list.indexOf(mdmadldsgrp),1);
        // ent_list.splice(ent_list.indexOf(mdmadldsgrp), 1);
        // entitlements = ent_list;
        gs.print('entitlements1 splice' +  ent_list);
    }


}
// gs.print('entitlements2' + entitlements);
 }

TIA!!
1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage
Mega Sage

@Ankita9793 

You can take this sample code as reference

var array_entitlements = ['a', 'b', 'c'];

const index = array_entitlements.indexOf('b');
if (index !== -1) {
  array_entitlements.splice(index, 1); // Removes 'b' at the found index
}

console.log(array_entitlements); // Output: ['a', 'c']

 

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

View solution in original post

3 REPLIES 3

SumanthDosapati
Mega Sage
Mega Sage

@Ankita9793 

You can take this sample code as reference

var array_entitlements = ['a', 'b', 'c'];

const index = array_entitlements.indexOf('b');
if (index !== -1) {
  array_entitlements.splice(index, 1); // Removes 'b' at the found index
}

console.log(array_entitlements); // Output: ['a', 'c']

 

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

Chaitanya ILCR
Kilo Patron

Hi @Ankita9793 ,

 

Try this

var entitlements = ['0678191f1b13a4d06c3c2f007e4bcb9b', '5b806c328396c6105fc86770deaad354'];
var mdmadldsgrp = '5b806c328396c6105fc86770deaad354';
var cat_item = 'Mobile - NEW';

if (cat_item == "Mobile - NEW") {
    var index = entitlements.indexOf(mdmadldsgrp);
    if (index > -1) {
        entitlements.splice(index, 1); // Remove the item at the found index
        gs.print('Updated entitlements: ' + entitlements);
    } else {
        gs.print('Group not found in entitlements.');
    }
}

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Arun_Manoj
Mega Sage

Hi @Ankita9793 ,

 

line 4 and 5

var cat_item = 'Mobile - NEW';

 

if (cat_item == "Mobile - NEW"); {
 
already hardcoded variable value , they what is need to check?
 
sys_id don't use directly in the script , please use properties to fetch the sys_id , ie, best properties
 

Please mark my answer as helpful/correct if it resolves your query.

Regards,

Arun Manoj