[{
id: '1',
name: '小王',
desc: '小王描述'
}, {
id: '2',
name: '小强',
desc: '小强描述'
}, {
id: '6',
name: '小红',
desc: '小红描述'
}, {
id: '9',
name: '小东',
desc: '小东描述'
}]
This is the data sent back from the background. How can I use a for
loop to turn the id
inside into a number? I don’t know how to write it, so embarrassed
The key point is to convert the string into a numerical value, either
parseInt
orNumber
can be converted. Or let the background pass the numerical type directly. Question: This is data obtained from the backend and is generally rendered to the page. Why does it need to be converted into a numerical value?Like this
But because of the object reference type, the id in the original array arr is also a numeric value
Just use parseInt to transfer directly and it will be ok
Let’s talk about the idea first:
1. First traverse the array
2. Traverse the object
3. Add attribute key-value pairs and delete old key-value pairs
Code below: