javascript - JSON: Add pointer -
i have automatially made json looks like:
{ "node_id_1": { "x": -87, "y": 149 }, "node_id_2": { "x": -24, "y": 50 }, "node_id_3": { "x": 55, "y": -32 } }
i have problems access specific node_id x , y values. idea automatically add "id:"
before e.g. "node_id_1"
, flatten array. give me hint how add "id:"
? or there elegant way access ids? thank lot! best regards
a possibility using es6, if attempting do? perhaps clarify question further description , examples of code using trying access data?
function clamp(value, min, max) { return math.min(math.max(value, min), max); } const jsonstring = '{"node_id_1":{"x":-87,"y":149},"node_id_2":{"x":-24,"y":50},"node_id_3":{"x":55,"y":-32}}'; const javascriptobject = json.parse(jsonstring); const mysortedarray = []; // depending on preference use array#map here // instead or create iterator object. (let key of object.keys(javascriptobject)) { mysortedarray.push({ id: key, value: object.assign({}, javascriptobject[key]) }); } mysortedarray.sort((a, b) => { // overkill, precise. return clamp(math.trunc(a.id.localecompare(b.id)), -1, 1); }); document.getelementbyid('out').textcontent = json.stringify(mysortedarray, null, 2); console.log(mysortedarray);
<pre id="out"></pre>
references:
Comments
Post a Comment