background

  • A simple and temporary form to fill in information was put on the local static HTML web page, and the data was also stored in script tags as variables. After the HTML was directly sent to the other party to fill in, the CDN filesaver.js was used to generate json files with processed formats, and I collected the files and threw them directly to the script to read. (Mainly because this is a brand new project, I have not done any server database, so I have to input the data by hand for the time being, so I wrote a small program to distribute the workload to the person collecting customer information, and I only need to collect the semi-finished products that can be directly sent to the script for batch processing)

  • For convenience, if a person has more than one mobile phone to fill in, write the two numbers in the same input separated by Spaces.

  • The collected data is formatted before the file is generated, and the phone fields are split into array elements

  • That is, data is structured as follows

/ / the original
let origin=[
	{
		name:'xx'.phone:'xx xx'},... ]/ / modified
let update=[
	{
		name:'xx'.phone: ['xx'.'xx']},... ]Copy the code

The problem found

  • ** automatically uses slice for deep copy, ** then finds that the first build file works fine, and an error is reported when you click Build without refreshing the page
  • After printing, it was found that the phone of the original data was also changed to the form of array, so it could not be processed again. An error was reported

At one point, I thought I had misremembered that Slice could not be deeply copied. I tried using concat and other methods, but found that none of them worked

  • Finally opened a new JS file to try to find a nested structure of the pot

Problem solving

It’s easy to know why, just solve for deep copy of nested structures.

Chose the easiest way:JSON.parse(JSON.stringify(origin))

  • Attention! JSON deep copy is an easy way to solve the nesting problem, but it has its drawbacks

Disadvantages of the JSON method of deep copy

I found a deep copy method on the Internet, but IT’s a one-time thing that I use temporarily to reduce rework, and the content is only strings, so I don’t want to do it.

After all, knowledge is not solid just gave such a problem ah…