1
I have looked at several examples on the NET but I am not able to solve this situation. I’d appreciate it if someone could help.
I need to convert a string into an Array with elements, I will receive this string from a database and based on it I need to mount the Array.
When I load the Array with fixed values it mounts exactly what I need as can be seen in the Debug print below, this is exactly how I need it.
But when I mount using the values (string) I took from the Bank is not mounting in the same way as the example of the figure above.
I set an example in https://codepen.io/egameiro/pen/JqqPxW in order to test.
Follow the full code I’m using.
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const circles = [{
id: '1',
x: 40,
y: 40,
radius: 5,
color: 'rgb(255,0,0)'
}, {
id: '2',
x: 100,
y: 70,
radius: 5,
color: 'rgb(0,0,255)'
}, {
id: '3',
x: 160,
y: 100,
radius: 5,
color: 'rgb(0,255,0)'
}];
circles.forEach(circle => {
ctx.beginPath();
ctx.arc(circle.x, circle.y, circle.radius, 0, 2 * Math.PI, false);
ctx.fillStyle = circle.color;
ctx.fill();
});
var bb = document.getElementById("LinhaPontos").value
const circles2 = [bb];
var aa = 0;
const novaArray = circles2.map(Object.values);
var bb = 0;
<div>
<input type="text" id="LinhaPontos" class="btn btn-info " size="40" value="{id: '1', x: 40, y: 40, radius: 5, color: 'rgb(255,0,0)'}" />
</div>
<canvas id="canvas" width="650" height="650" style="border: 1px solid black"></canvas>
And what would be the string?
– Luiz Felipe
Have you tried
circles2.map(circle => Object.values(JSON.parse(circle)))
?– Andre
But the information that’s on
value
was for 1 point ? and the others ? For the format you are inserting the ideal is to interpret as JSON.– Isac
Hi Isac the information that is in value is for a point. I just put as example, but usually will have several points.
– Eduardo
user140828.. tried and failed.
– Eduardo