0

how can i remove a Array from a JSON with a generated ID.

So i have a JSON Like:

{
    "Artikel":[
        { "id:1, artikelnr": "23453345", "anzahl": 22},
        { "id:2, artikelnr": "asd323", "anzahl": 2223},
        { "id:5, artikelnr": "dfsdf3893", "anzahl": 72},
        { "id:6, artikelnr": "asdikr38", "anzahl": 86},
        { "id:9, artikelnr": "2sad34533asd45", "anzahl": 10}
    ]
}

and i like to remove the json for example with the id 6, then the JSON should look like:

{
     "Artikel":[
         { "id:1, artikelnr": "23453345", "anzahl": 22},
         { "id:2, artikelnr": "asd323", "anzahl": 2223},
         { "id:5, artikelnr": "dfsdf3893", "anzahl": 72},
         { "id:9, artikelnr": "2sad34533asd45", "anzahl": 10}
     ]
}

how can i make this?

Aison
  • 157
  • 2
  • 10
  • You should restructure it first. Having `id:9, artikelnr` as a property name is not very useful. – str Jul 23 '19 at 07:43
  • @DanielKreiner is it your keys of object? `"id:2, artikelnr":`? maybe it looks like this? `"id":5, "artikelnr":dfsdf3893` – StepUp Jul 23 '19 at 08:08

3 Answers3

0

You can map over each object, split the key and check if id:6 exists:

const obj = { Artikel: [{ "id:1, artikelnr": "23453345", anzahl: 22 }, { "id:2, artikelnr": "asd323", anzahl: 2223 }, { "id:5, artikelnr": "dfsdf3893", anzahl: 72 }, { "id:6, artikelnr": "asdikr38", anzahl: 86 }, { "id:9, artikelnr": "2sad34533asd45", anzahl: 10 }] };

obj.Artikel = obj.Artikel.filter(o => Object.keys(o).every(i => i.split(', ')[0] !== 'id:6'))
console.log(obj)

You could even go as far as splitting again to use a blacklist:

const obj = { Artikel: [{ "id:1, artikelnr": "23453345", anzahl: 22 }, { "id:2, artikelnr": "asd323", anzahl: 2223 }, { "id:5, artikelnr": "dfsdf3893", anzahl: 72 }, { "id:6, artikelnr": "asdikr38", anzahl: 86 }, { "id:9, artikelnr": "2sad34533asd45", anzahl: 10 }] };

const blacklist = [1, 6]

obj.Artikel = obj.Artikel.filter(o => Object.keys(o).every(i => !blacklist.includes(+i.split(', ')[0].split(':')[1])))
console.log(obj)
Kobe
  • 6,226
  • 1
  • 14
  • 35
0

You could create a dynamic regex based on the id you want remove. Then, filter the array to get all the objects which don't have any keys which have the given pattern

const input={Artikel:[{"id:1, artikelnr":"23453345",anzahl:22},{"id:2, artikelnr":"asd323",anzahl:2223},{"id:5, artikelnr":"dfsdf3893",anzahl:72},{"id:6, artikelnr":"asdikr38",anzahl:86},{"id:9, artikelnr":"2sad34533asd45",anzahl:10}]},
    idToRemove = 6,
    regex = new RegExp(`id:${idToRemove}`),
    Artikel = input.Artikel.filter(o => !Object.keys(o).some(k => regex.test(k)))

console.log({ Artikel } )
adiga
  • 34,372
  • 9
  • 61
  • 83
-1

A simpler way to do it, would be:

const j = {"Artikel":[
  { "id":1, "artikelnr": "23453345", "anzahl": 22},
  { "id":2, "artikelnr": "asd323", "anzahl": 2223},
  { "id":5, "artikelnr": "dfsdf3893", "anzahl": 72},
  { "id":6, "artikelnr": "asdikr38", "anzahl": 86},
  { "id":9, "artikelnr": "2sad34533asd45", "anzahl": 10}
]}


j["Artikel"].forEach(function(item, index){
 if(item["id"] == 6){
  j["Artikel"].splice(index, 1)
 }
});

console.log(j)
Luc
  • 393
  • 7
  • 19