Reputation: 443
I have an array of objects called active_filters
. It contains a key called value which can contain one or more values (which are separated by commas if there are multiples). I'm trying to remove a certain code from the value key using findIndex. I'm not sure what I'm doing wrong...would appreciate any help. Thanks!
remove_code = "SFMR";
active_filters[0] = {id: "data-type", value: "SFAR,CFAR,IFAR,SFMR,FFAR,PDAR,MCMR,EDMR,CDMR,ECMR,EDAR,CDAR,MDMR", type: "filtered-out-by-car-type"};
Code:
var index = active_filters.findIndex(function(e) { return e.value.split(",").indexOf(remove_code) && e.id === _id });
if (index > -1)
active_filters.splice(index, 1);
Upvotes: 2
Views: 75
Reputation: 33726
I'm trying to remove a certain code from the value key using findIndex.
You're splicing the entire array active_filters
, what you need to do is split the filter's value and splice it according the remove_code
values. To accomplish that, make a reverse loop and for each match execute the function splice
.
Finally, join the values again separating them with comma.
var remove_code = "SFMR",
_id = 'data-type',
active_filters = [{id: "data-type", value: "SFAR,CFAR,IFAR,SFMR,FFAR,PDAR,MCMR,EDMR,CDMR,ECMR,EDAR,CDAR,MDMR", type: "filtered-out-by-car-type"}];
active_filters.forEach(function(f) {
if (f.id === _id) {
var values = f.value.split(','),
length = values.length;
while (length--) if (values[length] === remove_code) values.splice(length, 1);
f.value = values.join();
}
});
console.log(active_filters);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://codepen.io/egomezr/pen/dmLLwP.js"></script>
Upvotes: 0
Reputation: 2024
You could find the index the slice around it and set that to the value. Create a function that takes in the characters that you want to remove then find the index of those characters. After that you can slice from 0 to the specified index, then from the index + 5
(5 to include the comma) to finish out the remainder of the string. Here's an example of that:
const remove_code = "SFMR";
let active_filters = [];
active_filters.push({id: "data-type", value: "SFAR,CFAR,IFAR,SFMR,FFAR,PDAR,MCMR,EDMR,CDMR,ECMR,EDAR,CDAR,MDMR", type: "filtered-out-by-car-type"});
function removeChars(chars){
const index = active_filters[0].value.indexOf(remove_code);
let newString;
newString = active_filters[0].value.slice(0, index);
newString += active_filters[0].value.slice(index+5);
return newString;
}
active_filters[0].value = removeChars(remove_code);
console.log(active_filters[0]);
Upvotes: 0
Reputation: 68933
You can do the following with filter()
, map()
, indexOf()
and splice()
:
var remove_code = "SFMR";
var _id = "data-type";
var active_filters = [{id: "data-type", value: "SFAR,CFAR,IFAR,SFMR,FFAR,PDAR,MCMR,EDMR,CDMR,ECMR,EDAR,CDAR,MDMR", type: "filtered-out-by-car-type"}];
var active_filters = active_filters.filter(i => i.id==_id).map(function(e) {
var temp = e.value.split(",");
var index = temp.indexOf(remove_code);
temp.splice(index,1);
e.value = temp.join();
return e;
});
console.log(active_filters);
Upvotes: 0
Reputation: 191976
Iterate the array with [Array.findIndex()
][1] to find the element by id
. If the element was found, split the value by comma. Use Array.filter()
to remove the code, and join back. Assign the result back to the value property.
var remove_code = "SFMR";
var _id = 'data-type';
var active_filters = [{id: "data-type", value: "SFAR,CFAR,IFAR,SFMR,FFAR,PDAR,MCMR,EDMR,CDMR,ECMR,EDAR,CDAR,MDMR", type: "filtered-out-by-car-type"}];
var index = active_filters.findIndex(function(o) {
return o.id === _id;
});
if(index !== -1) {
active_filters[index].value = active_filters[index].value.split(',').filter(function(s) {
return s !== remove_code;
}).join();
}
console.log(active_filters);
Upvotes: 1