I'm receiving the json data in the below format
[
{
"key":4,
"data":{
"choice":[
{
"id":"1.1"
},
{
"id":"1.2"
}
],
"questionId":"1"
}
},
{
"key":4,
"data":{
"choice":[
{
"id":"1.1.1.1"
},
{
"id":"1.1.1.2"
},
{
"id":"1.1.1.3",
}
],
"questionId":"1.1"
}
},
{
"key":4,
"data":{
"choice":[
{
"id":"1.1.1.3.1.1"
},
{
"id":"1.1.1.3.1.2"
}
],
"questionId":"1.1.1.3"
}
},
{
"key":4,
"data":{
"choice":[
{
"id":"1.1.1.3.1.1.1.1"
},
{
"id":"1.1.1.3.1.1.1.2"
}
],
"questionId":"1.1.1.3.1.1"
}
},
{
"key":5,
"data":{
"choice":[
{
"id":"2.1"
},
{
"id":"2.2"
}
],
"questionId":"2"
}
},
{
"key":5,
"data":{
"choice":[
],
"questionId":"2.2"
},
}
]
and I want to delete the record from this array based on multiple condition which is as followed
- The "key" ,suppose i pass key as 4 the only the records which has key as 4 should be considered for deletion nothing from key as 5 should be deleted
- the second is the id suppose the id(this is inside choice) I get is "1.1.1.2" then only the records after that should be deleted
so for eg I get key as 4 and id as "1.1.1.2" then the expected output should be as followed
[
{
"key":4,
"data":{
"choice":[
{
"id":"1.1"
},
{
"id":"1.2"
}
],
"questionId":"1"
}
},
{
"key":4,
"data":{
"choice":[
{
"id":"1.1.1.1"
},
{
"id":"1.1.1.2"
},
{
"id":"1.1.1.3",
}
],
"questionId":"1.1"
}
},
{
"key":5,
"data":{
"choice":[
{
"id":"2.1"
},
{
"id":"2.2"
}
],
"questionId":"2"
}
},
{
"key":5,
"data":{
"choice":[
],
"questionId":"2.2"
},
}
]
I tried using splice inside forEach but seems with splice there is an unusual behavior when used inside foreach anyother approach for the same
My code that i was trying
this.followUpQues.forEach(function(val,index,object) {
if(val.data.choice.filter(y=>y.id === item.id).length >= 1) {
keyNumber = val.key;
valueIndex = index;
}
})
so after getting key and index i tried to delete the records after the index found and having key value that is identified,but using splice inside the foreach has weird behaviour