1

I would like to remove specific value from an array , I have tried following script for delete value from an array but that not working.

HTML that contain values of an array

<input type="hidden" class="image_array" id="image_array" name="image_array[]" value="20200207050212.jpg,20200207050214.jpg">

So my array value is 20200207050212.jpg,20200207050214.jpg & I would like to remove 20200207050212.jpg from this array but not remove when i used following script.

1 Way

$(document).ready(function(){
  $(document).on("click", '.profile_delete_image', function(){
        var getImageName = $(this).attr('data-imagename');
        console.log(getImageName)
        var getImageArray =$('.image_array').val();
        var checkValueExist = (getImageArray.indexOf(getImageName) > -1);
        if(checkValueExist == true){
          var itemtoRemove = getImageName;
          getImageArray = $.grep(getImageArray, function(value) {
            return value != itemtoRemove;      
              console.log(getImageArray)
          });
        }
    });
})

2 Way

$(document).ready(function(){
  $(document).on("click", '.profile_delete_image', function(){
        var getImageName = $(this).attr('data-imagename');
        console.log(getImageName)
        var getImageArray =$('.image_array').val();
        var checkValueExist = (getImageArray.indexOf(getImageName) > -1);
        if(checkValueExist == true){
          var itemtoRemove = getImageName;
          getImageArray.splice(1,1);
        }
    });
})

NOte: when i do console.log it's return separated value like

2
0
2
0
0
2
0
7
0
5
0
2
1
2
j
p
g

So I don't want this i only want to remove given value from array and return an array with removed value.

2 Answers 2

1

Just use split() to make string as array.

var getImageName = '20200207050212.jpg' // Get your delete image name. 
var getImageArray = [];
getImageArray = $('.image_array').val().split(',');
getImageArray = getImageArray.filter(e => e !== getImageName);

console.log(getImageArray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="hidden" class="image_array" id="image_array" name="image_array[]" value="20200207050212.jpg,20200207050214.jpg">

Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at the snippet, This is a simple example, here we have an array of images, when the button is clicked, it will get the images to delete from image_array.

Checks for if images_array has value, and convert it to array using .split(). Then iterate through the array and delete if the value in images array matched.

var $ = jQuery;
var images = ['20200207050212.jpg', '20200207050214.jpg', 'abc.jpg', 'image_123123.jpg'];
$('#delete_image').on('click', function() {
  console.log('before delete', images);
  var image_arr = $('#image_array').val();
  image_arr = image_arr ? image_arr.split(',') : image_arr;
  if (image_arr.length) {
    image_arr.forEach(function(img) {
      if (images.indexOf(img) !== -1) images.splice(images.indexOf(img), 1)

    })
    console.log('after delete', images);
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<input type="hidden" class="image_array" id="image_array" name="image_array[]" value="20200207050212.jpg,20200207050214.jpg">

<button id="delete_image">Delete Image </button>

5 Comments

I always get following Array after removing the element = ',2,0,2,0,0,2,0,7,0,5,3,5,4,2,.,j,p,g,',,,',2,0,2,0,0,2,0,7,0,5,3,5,4,4,.,j,p,g,'
@David did you run my snippet ??
Yes , but when i tried i got same thing that i have just mentioned in comment
By the way where you pass deleted image name in your code? \
@David did you meant to remove 20200207050212.jpg from this input ?? <input type="hidden" class="image_array" id="image_array" name="image_array[]" value="20200207050212.jpg,20200207050214.jpg">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.