I have the following string
[{"idServicio":10},{"idServicio":11},{"idServicio":12}]
and what I want to do is remove an element from the value of the idService, for example removing the id value 11, and something like this:
[{"idServicio":10},{"idServicio":12}]
Could you help me with the method substr
but I wanted to know if there is a method or function to which I pass the value of the id and delete it in a single line of code, without worrying about the location of the element and the commas.
For me it is a JSON, but the JS console tells me that it is a string (could it be both?) That is why the title of the question is like this. If I have misconceptions I appreciate any comment, I'm taking my first steps.
Thank you very much
You can help yourself from the method
filter()
which:Edition
You need to make use of
JSON.parse()
since at the beginning you have a text string and with this method we will obtain a JavaScript object that will be capable of being iterated by the method that I am proposing in this answerIt will allow you to generate a new vector with the elements of the original vector that meet a certain condition.
For example in your scenario, you can: Filter by the key
idServicio
when it is different from 11, so that the variabledatos
becomes the new vector that has all the elements of the previous one except the one with id 11Being that way:
How about I leave you this function, I hope it works for you:
splice
is in charge of deleting the found object:1st parameter: will delete the object of index i.
2nd parameter: deletes the number of objects, in our case only 1. Optional
3rd parameter also allows adding.