DVGY
#1
What is wrong i am doing ?
const object1 = {
1: {
top: Math.floor(Math.random() * 100) + 20,
left: Math.floor(Math.random() * 200) + 100,
title: `Card G_${id}`
},
2: {
top: Math.floor(Math.random() * 100) + 20,
left: Math.floor(Math.random() * 200) + 100,
title: `Card G_${id}`
},
3: {
top: Math.floor(Math.random() * 100) + 20,
left: Math.floor(Math.random() * 200) + 100,
title: `Card G_${id}`
},
4: {
top: Math.floor(Math.random() * 100) + 20,
left: Math.floor(Math.random() * 200) + 100,
title: `Card G_${id}`
},
};
const newObj = {
...object1,
Object.keys(object1).map(key => {
if(object1[key]===1){
object1[key]["top"]=88;
object1[key]["left"]=88;
}
return object[key];
}
)
}
console.log(newObj)
ilenia
#2
I don’t know what it should do.
What’s happening, what should be happening instead?
DVGY
#3
I am trying to change top
value and left
value in the object1
object
1: {
top: Math.floor(Math.random() * 100) + 20,
left: Math.floor(Math.random() * 200) + 100,
title: `Card G_${id}`
},
So i am destructing it first then mapping it and changing the value.
Is there something wrong ?
ilenia
#4
There is a syntax error in the newObj
you can’t put Object.keys
as a property, if you want that to be a property of the object you need to give a property name to it
Are you interested in getting an array of the object[key]
elements?
using map to change stuff outside of it is not really a good thing to do, as it is causing side effects
DVGY
#5
I am interested in changing value of object[1][“Top”]
I am interested in changing value of object[1][“left”]
How can i do it without causing side effects ?
ilenia
#6
use forEach
, map is to use if you are interested in the returned array