How do i remove a particular number of an array of various strings?

Related searches

Suppose if I have data like the following array

"groupMapData": [
        {
            "menu_id": 17,
            "menu_name_message_id": "menuName.viewgroups",
            "access_role_ids": "3,1,4"
        },
        {
            "menu_id": 18,
            "menu_name_message_id": "menuName.creategroup",
            "access_role_ids": "2,1,4,3"
        }
    ]


checkboxfunction = (menuName, role, menuname) => {
        return menuname.some(menu => {
            let nameMatches = menu['menu_name_message_id'] === menuName,
                roleIncluded = menu['access_role_ids'].split(',')
                    .map(n => parseInt(n, 10))
                    .includes(role)
            return nameMatches && roleIncluded
        })
    }

findIndexfunction = (menuname,data) => {
    return data.findIndex(i => {
        return i.menu_name_message_id == menuname
    })
}


handleChange = (menuname, role, data) => event => {
    debugger
    let old_index_value=this.findIndexfunction(menuname, data)
    let old_roles=data[old_index_value].access_role_ids

    if (old_index_value) {
        if(event.target.checked){
            old_roles=old_roles.concat(role)
        }else{
            old_roles=old_roles.splice(old_index_value,1)  
        }
    }else{
        this.setState({ data: data })
    }

}




checked={groupMapData.length ? this.checkboxfunction("menuName.viewgroups", 2, groupMapData) : false}


 onChange={this.handleChange('menuName.viewgroups', 2, groupMapData)}

I am sending data,role, menuname from handlechange, now i want to change checkbox value based on the accessrole.

if its there in the accessrole, it has to be checked else unchecked.

Ohk let's use map

access_role_details.map((x)=>{
let role = x.access_role_ids
let roleArray = role.split(',');

//now we filter to remove the number you don't like eg. 4

roleArray.filter(k=>k != 4)

//now convert the array to a string and put it back

x.access_role_ids = roleArray.toString();

//return x now

return x;

}

Remove a specific string from an array of string, You can't remove anything from an array - they're always fixed length. Once you've created an array of length 3, that array will always have length 3. Collections like this are generally much more flexible than working with arrays directly. i know remove value from arraylist we use "list.remove()" but i need is there any property to remove string in string array – Vicky Oct 10 '12 at 5:14 @Vicky: No. There is no such property.

var obj = {"access_role_details": [
      {
          "menu_id": 17,
          "menu_name_message_id": "menuName.viewgroups",
          "access_role_ids": "3,1,4"
      },
      {
          "menu_id": 18,
          "menu_name_message_id": "menuName.creategroup",
          "access_role_ids": "2,1,4,3"
      }
  ]
          };

var objData = obj.access_role_details[0];
var removeItem = "1";

var tempArr = objData.access_role_ids.split(","); // convert comma separated to array

var index = tempArr.indexOf(removeItem);
if(index !=-1){
  tempArr.splice(index,1);
}
objData.access_role_ids = tempArr.join(); // convert array to comma separated value

console.log(obj);

Remove an Element at specific index from an Array in Java , The task is to remove an element at a specific index from the array. public static void main(String[] args) mapToInt(Integer::intValue) Get hold of all the important DSA concepts with the DSA Self Paced Course at a� To also support double I would do something like this:. public static bool IsNumeric(string input, NumberStyles numberStyle) { double temp; return Double.TryParse(input, numberStyle, CultureInfo.CurrentCulture, out temp); }

You need to convert string to array and remove the specific element and again join the array to string.

We can achieve this using string operation but a better way is to convert it into the array and then perform the deletion operation. It will give the correct result to you.

refer following code :

var myObj = JSON.parse(`{"access_role_details": [
              {
                  "menu_id": 17,
                  "menu_name_message_id": "menuName.viewgroups",
                  "access_role_ids": "3,1,4"
              },
              {
                  "menu_id": 18,
                  "menu_name_message_id": "menuName.creategroup",
                  "access_role_ids": "2,1,4,3"
              }
          ]}`);


        var tempArr = [];
        var ResultArr = [];
        var access_role_id = 1;//Value you want to remove from the access_role_ids 
        for (var i = 0; i < myObj.access_role_details.length; i++)
        {
             tempArr = [];
             ResultArr = [];
             // split method to covert into array
             tempArr = myObj.access_role_details[i].access_role_ids.split(',');
             ResultArr = tempArr.filter(item => item != access_role_id);
             //join method to convert it into string with ',' seprated
             myObj.access_role_details[i].access_role_ids = ResultArr.join(',');
        }
        console.log(JSON.stringify(myObj))

How to Remove an Element from Array in Java with Example, There is no direct way to remove elements from an Array in Java. Though The ArrayUtils also provided several overloaded remove() method for the different type of primitive arrays like int, long, float and double. public static void main( String args[]) { How to count the number of leaf nodes in a given binary tree in Java? 2.2. Remove all occurrences of element. We cannot directly remove all occurrences of any element from list using remove() method. We can use removeAll() method for this purpose. Java program to remove all the occurrences of an object from the arraylist.

How to Remove an Element from an Array in Java? Example , Problem: Given an array and a value, write a function to remove all instances of void main(String args[]) { System.out.println("Test #1 : General case to remove� Arrays do not have built-in deletion capabilities. Use an ArrayList of strings (or List<string> in .NET 2.0), and then call the RemoveAt method to remove the elements you have to. If you are going to be doing a lot of deleting in a large list, though, consider using a linked list.

Java exercises: Remove a specific element from an array, Write a Java program to remove a specific element from an array. Arrays; public class Exercise7 { public static void main(String[] args) { int[] my_array toString(my_array)); // Remove the second element (index->1, value->14) of the array int removeIndex Resetting will undo all of your current changes. Using Splice to Remove Array Elements in JavaScript. The splice method can be used to add or remove elements from an array. The first argument specifies the location at which to begin adding or removing elements. The second argument specifies the number of elements to remove.

Get the array and the index. Form an ArrayList with the array elements. Remove the specified index element using remove() method. Form a new array of the ArrayList using mapToInt() and toArray() methods. Return the formed array. Below is the implementation of the above approach:

Comments
  • On what condition you want to remove?
  • Okay I want to change the status of checkbox actually <Checkbox checked={groupMapData.length ? this.checkboxfunction("menuName.creategroup", 2, groupMapData) : false} onChange={this.handleChange('menuName.creategroup', 2, groupMapData)} //value= "General Admin can create new group" color="primary" />
  • Please create a minimal reproducible example
  • added in question
  • can i know what is this ? ResultArr = tempArr.filter(item => item != access_role_id);
  • tempArr is containing the array of access_role_ids. I am applying the filter operation on array of access_role_ids to remove the particular access_role_id from the access_role_ids array. filter method will return the array with removed particular access_role_id.
  • there is something wrong with this syntax item => item != access_role_id
  • Filter method will return the all array element expect access_role_id. access_role_id will be the id which you want to remove from the access_role_ids.