K
K
kuzubina2020-01-26 15:03:20
Node.js
kuzubina, 2020-01-26 15:03:20

How to clear an array in a file using the fs module?

the file has an array of this type

{
  "event": [
  	{
  		"id": "1"
  	},
  	{
  		"id": "1"
  	},
  	{
  		"id": "1"
  	}
  ]
}

how can it be cleared using the FS module to get such an empty array
{
  "event": []
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2020-01-26
@kuzubina

Read the file, parse json, set an empty array as the value of the property you are interested in, turn the object into json, write the file.

S
Shohruh Shaimardonov, 2020-01-26
@joeberetta

Use a regular expression, look for the desired text and replace it

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question