I have an array containing 7 objects named is NewClass.
When I make some changes to the objects, the object has lost its original NewClass name.
Like that:
In arrray[6] lost NewClass name.
So is there any way I can keep the NewClass name for that object?
Here is my function to handle this array:
JavaScript
x
21
21
1
useEffect(() => {
2
console.log(layers);
3
if (selectedLayerIndex && layers.length > 0) {
4
5
console.log(
6
'x',
7
layers.map((x) => {
8
if (x.feature.properties.editLayerId === selectedLayerIndex) {
9
console.log(x);
10
11
return (x = {
12
x,
13
options: { x.options, color: 'cyan', fillColor: 'cyan' },
14
});
15
}
16
return x;
17
})
18
);
19
20
}
21
}, [selectedLayerIndex]);
Thank you so much!
Advertisement
Answer
by using the syntax x = {...x, option: {/* some changes to the class */}
here, you change the class to a object, which cause a lost of your class name.
If you want to keep it as a class, please use instance.attribute instead, see the following example
JavaScript
1
18
18
1
class Demo {
2
constructor(color){
3
this.color = color;
4
}
5
}
6
7
var array = [new Demo('red'),new Demo('red'),new Demo('red'),new Demo('red'),new Demo('red'),new Demo('red'),new Demo('red')];
8
9
console.log('original array', array);
10
console.log('');
11
12
array[6] = {array[6], color: 'blue'};
13
console.log(array); // array[6] have been change to a object
14
console.log('');
15
16
array[5].color = 'green';
17
console.log(array); // array[5] is still a class
18
console.log('');