Reputation: 223
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.
Upvotes: 1
Views: 83
Reputation: 22323
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">
Upvotes: 1
Reputation: 6130
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>
Upvotes: 0