0

I have some JSON that looks like this

var js = '{
            "person" : {
                        "firstname" : "Steve",
                        "lastname" : "Smith",
                        "other": [
                                    { 
                                       "age" : "32",
                                       "deceased" : false
                                    },
                                    { 
                                       "age" : "14",
                                       "deceased" : false
                                    },
                                    { 
                                       "age" : "421",
                                       "deceased" : false
                                    }
                                 ]
                       }
         }'

I know how I delete all of "other" by doing this

var j = JSON.parse(js)
delete j[person.other]

but what I really want is to delete the "other" node with a condition that is age = 14.

The result JSON I am looking for is

{
            "person" : {
                        "firstname" : "Steve",
                        "lastname" : "Smith",
                        "other": [
                                    { 
                                       "age" : "32",
                                       "deceased" : false
                                    },
                                    { 
                                       "age" : "421",
                                       "deceased" : false
                                    }
                                 ]
                       }
}

I have looked at the delete operator here and it does not provide a conditional.

Sorry if this is too simple a question. I am learning.

Thanks

Steve
  • 89
  • 1
  • 8
  • 1
    `person.other` is an `Array` -> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array – Andreas Feb 08 '22 at 14:02
  • Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – evolutionxbox Feb 08 '22 at 14:03
  • You don't want to `delete` the whole array. You want to [`filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) the existing array. – JSON Derulo Feb 08 '22 at 14:05

2 Answers2

1

You can use a filter on the array doing something like

j[person.other].filter(x => x.age != 14)

Doc on how filter works more in depth here :

https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

cdelaby
  • 11
  • 2
1

The best approach to this would be not to alter the json, but create another one instead:

const filtered = { ...json, other: json.other.filter(item => item.age !== "14") }

If you really need to alter the json, you can do the following:

let index = json.other.findIndex(item => item.age === "14")
while (index !== -1) {
  json.other.splice(index, 1)
  index = json.other.findIndex(item => item.age === "14")
}
Tiago Peres França
  • 3,056
  • 2
  • 21
  • 23