I am working with an array structure that looks like this:
[
{key: 4, data: {…}, qText: {…}},
{key: 4, data: {…}, qText: {…}, isVisible: false},
{key: 5, data: {…}, qText: {…}, isVisible: false},
{key: 4, data: {…}, qText: {…}, isVisible: false}
]
My goal is to remove elements from the array based on two conditions:
If I receive a key value input of 4, then all keys with value 4 should be deleted.
The second scenario involves deleting a specific key and index when certain criteria are met within the nested data. For example, in the given array, if I receive an index of 1 and a key of 4, only that particular element should be removed.
I initially attempted to solve this problem using multiple loops, but it was not very effective. What would be the most efficient approach performance-wise to achieve this? 1) Firstly, I tried to find the keys as shown below:
const deleteKey = this.followUpQues.filter(quest =>
quest.data.choice.some(y => y.id === item.id)
).map(quest =>
quest.key
)
2) Next, I attempted to determine the index using the following code:
this.followUpQues.forEach(function(val, index) {
if (val.data.choice.filter(y => y.id === item.id).length >= 1) {
keyNumber = val.key;
valueIndex = index;
}
})
this.followUpQues.splice(valueIndex + 1, this.followUpQues.length - (valueIndex + 1))