Add the X values in Json with respect to ImageWhich “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?Add table row in jQueryHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?Why can't Python parse this JSON data?How can I pretty-print JSON using JavaScript?Parse JSON in JavaScript?
Why is it that I can sometimes guess the next note?
What is the highest possible scrabble score for placing a single tile
Strong empirical falsification of quantum mechanics based on vacuum energy density
Picking the different solutions to the time independent Schrodinger eqaution
How can "mimic phobia" be cured or prevented?
Using substitution ciphers to generate new alphabets in a novel
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
Open a doc from terminal, but not by its name
How can I write humor as character trait?
How to explain what's wrong with this application of the chain rule?
Can disgust be a key component of horror?
Why Shazam when there is already Superman?
Pre-mixing cryogenic fuels and using only one fuel tank
Angel of Condemnation - Exile creature with second ability
Does IPv6 have similar concept of network mask?
How do you make your own symbol when Detexify fails?
15% tax on $7.5k earnings. Is that right?
Are Captain Marvel's powers affected by Thanos' actions in Infinity War
Mixing PEX brands
Can a stoichiometric mixture of oxygen and methane exist as a liquid at standard pressure and some (low) temperature?
What is the evidence for the "tyranny of the majority problem" in a direct democracy context?
Does malloc reserve more space while allocating memory?
Why does a simple loop result in ASYNC_NETWORK_IO waits?
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
Add the X values in Json with respect to Image
Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?Add table row in jQueryHow do I format a Microsoft JSON date?Can comments be used in JSON?How can I pretty-print JSON in a shell script?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?Why can't Python parse this JSON data?How can I pretty-print JSON using JavaScript?Parse JSON in JavaScript?
In a Json, there are multiple image sources as "src" : "image.png", , "src" : "image2.png",
For image.png , right now i am fetching X value as "40" [3rd position in below image]
For image2.png , right now i am fetching X value as "100" [6th position in below image]
Requirement :
Instead of that, i need to add 1st (10) + 3rd (40) + 4th (50) positions values and fetch final X value for "src" : "image.png"
, as 100 [10+40+50] and
"src" : "image1.png"
= 1st (10) + 6th (100) + 7th (105) positions & final value of X is 215....
Codepen : https://codepen.io/kidsdial/pen/zbKaEJ
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
for (i = 1; i < layer2.length; i++)
var x = layer2[i].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
javascript json
add a comment |
In a Json, there are multiple image sources as "src" : "image.png", , "src" : "image2.png",
For image.png , right now i am fetching X value as "40" [3rd position in below image]
For image2.png , right now i am fetching X value as "100" [6th position in below image]
Requirement :
Instead of that, i need to add 1st (10) + 3rd (40) + 4th (50) positions values and fetch final X value for "src" : "image.png"
, as 100 [10+40+50] and
"src" : "image1.png"
= 1st (10) + 6th (100) + 7th (105) positions & final value of X is 215....
Codepen : https://codepen.io/kidsdial/pen/zbKaEJ
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
for (i = 1; i < layer2.length; i++)
var x = layer2[i].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
javascript json
add a comment |
In a Json, there are multiple image sources as "src" : "image.png", , "src" : "image2.png",
For image.png , right now i am fetching X value as "40" [3rd position in below image]
For image2.png , right now i am fetching X value as "100" [6th position in below image]
Requirement :
Instead of that, i need to add 1st (10) + 3rd (40) + 4th (50) positions values and fetch final X value for "src" : "image.png"
, as 100 [10+40+50] and
"src" : "image1.png"
= 1st (10) + 6th (100) + 7th (105) positions & final value of X is 215....
Codepen : https://codepen.io/kidsdial/pen/zbKaEJ
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
for (i = 1; i < layer2.length; i++)
var x = layer2[i].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
javascript json
In a Json, there are multiple image sources as "src" : "image.png", , "src" : "image2.png",
For image.png , right now i am fetching X value as "40" [3rd position in below image]
For image2.png , right now i am fetching X value as "100" [6th position in below image]
Requirement :
Instead of that, i need to add 1st (10) + 3rd (40) + 4th (50) positions values and fetch final X value for "src" : "image.png"
, as 100 [10+40+50] and
"src" : "image1.png"
= 1st (10) + 6th (100) + 7th (105) positions & final value of X is 215....
Codepen : https://codepen.io/kidsdial/pen/zbKaEJ
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
for (i = 1; i < layer2.length; i++)
var x = layer2[i].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
for (i = 1; i < layer2.length; i++)
var x = layer2[i].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
for (i = 1; i < layer2.length; i++)
var x = layer2[i].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
javascript json
javascript json
asked Mar 5 at 5:26
vickey colorsvickey colors
204220
204220
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
Using a recursive function you can find all the src
and its corresponding summed x
values.
Below is a crude example. Refactor as you see fit.
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getAllSrc(layers)
let arr = [];
layers.forEach(layer =>
if(layer.src)
arr.push(src: layer.src, x: layer.x);
else if(layer.layers)
let newArr = getAllSrc(layer.layers);
if(newArr.length > 0)
newArr.forEach((src, x) =>
arr.push(src, x: (layer.x + x));
);
);
return arr;
function getData(data)
let arr = getAllSrc(data.layers);
for (let src, x of arr)
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
Here is the CodePen for the same: https://codepen.io/anon/pen/Wmpvre
Hope this helps :)
add a comment |
You can do that using recursion. While going though nested obj you could parent x
value each item in layers
and check if the element in layers
have src
property add the previous x
value to it.
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function changeData(obj,x=0)
if(obj.src) obj.x += x;
if(obj.layers)
for(const item of obj.layers)
changeData(item,x+obj.x
changeData(jsonData);
function showData(obj)
if(obj.src) document.body.innerHTML += `src:$obj.src<br>
x:$obj.x<br>`;
if(obj.layers)
for(let i of obj.layers) showData(i)
showData(jsonData);
console.log(jsonData);
Thanks again for your help, As the other answer gave answer before 3 minutes, i accepted that answer....
– vickey colors
Mar 11 at 5:10
@vickeycolors Time doesn't matter in giving answer. The thing which matters is short and efficient and I think mine code is better. And if you was not to accept my answer you shouldn't have asked me that much help.well if you think his code is better so no problem
– Maheer Ali
Mar 11 at 5:26
I agree, i will rethink about it and i will take the final decision, thanks.....
– vickey colors
Mar 11 at 5:27
add a comment |
Here is my solution with recursion and mutation. You can cloneDeep the array before if you don't want to mutate it directly.
// Code
function recursion(obj, currentX = 0)
if(obj.src) obj.x = currentX
else if(obj.layers)
obj.layers.forEach(subObj => recursion(subObj, (currentX + subObj.x)))
// Data
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
// Use
recursion(jsonData)
console.log(jsonData)
add a comment |
let jsonData =
"layers": [
"x": 10,
"layers": [
"x": 20,
"y": 30,
"name": "L2a"
,
"x": 40,
"layers": [
"x": 50,
"src": "image.png",
"y": 60,
"name": "L2b-1"
,
"x": 70,
"y": 80,
"name": "L2b-2"
],
"y": 90,
"name": "user_image_1"
,
"x": 100,
"layers": [
"x": 105,
"src": "image2.png",
"y": 110,
"name": "L2C-1"
,
"x": 120,
"y": 130,
"name": "L2C-2"
],
"y": 140,
"name": "L2"
],
"y": 150,
"name": "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
let y = layer1[0].x;
for (i = 1; i < layer2.length; i++)
var x = y;
x += layer2[i].x;
x += layer2[i].layers[0].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
add a comment |
Here is My Solution. Check this out
let jsonData =
"layers" : [
"x" : 10, //
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40, //
"layers" : [
"x" : 50, //
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
var dataObj = [];
var x, src;
function getData(data)
let layer1 = data.layers;
for(var i = 0; i < layer1.length; i++)
if(x === 0)
x = layer1[i].x;
continue;
x = layer1[i].x;
if(typeof layer1[i].layers !== 'undefined')
createOutput(layer1[i].layers);
return dataObj;
function createOutput(dataArray)
if(typeof dataArray === 'undefined')
dataObj.push(x: x, src: src);
x = 0;
getData(jsonData);
return;
dataArray.forEach(element => typeof element.src !== 'undefined')
x += element.x;
src = element.src;
createOutput(element.layers);
)
console.log(JSON.stringify(getData(jsonData)));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54995969%2fadd-the-x-values-in-json-with-respect-to-image%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using a recursive function you can find all the src
and its corresponding summed x
values.
Below is a crude example. Refactor as you see fit.
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getAllSrc(layers)
let arr = [];
layers.forEach(layer =>
if(layer.src)
arr.push(src: layer.src, x: layer.x);
else if(layer.layers)
let newArr = getAllSrc(layer.layers);
if(newArr.length > 0)
newArr.forEach((src, x) =>
arr.push(src, x: (layer.x + x));
);
);
return arr;
function getData(data)
let arr = getAllSrc(data.layers);
for (let src, x of arr)
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
Here is the CodePen for the same: https://codepen.io/anon/pen/Wmpvre
Hope this helps :)
add a comment |
Using a recursive function you can find all the src
and its corresponding summed x
values.
Below is a crude example. Refactor as you see fit.
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getAllSrc(layers)
let arr = [];
layers.forEach(layer =>
if(layer.src)
arr.push(src: layer.src, x: layer.x);
else if(layer.layers)
let newArr = getAllSrc(layer.layers);
if(newArr.length > 0)
newArr.forEach((src, x) =>
arr.push(src, x: (layer.x + x));
);
);
return arr;
function getData(data)
let arr = getAllSrc(data.layers);
for (let src, x of arr)
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
Here is the CodePen for the same: https://codepen.io/anon/pen/Wmpvre
Hope this helps :)
add a comment |
Using a recursive function you can find all the src
and its corresponding summed x
values.
Below is a crude example. Refactor as you see fit.
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getAllSrc(layers)
let arr = [];
layers.forEach(layer =>
if(layer.src)
arr.push(src: layer.src, x: layer.x);
else if(layer.layers)
let newArr = getAllSrc(layer.layers);
if(newArr.length > 0)
newArr.forEach((src, x) =>
arr.push(src, x: (layer.x + x));
);
);
return arr;
function getData(data)
let arr = getAllSrc(data.layers);
for (let src, x of arr)
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
Here is the CodePen for the same: https://codepen.io/anon/pen/Wmpvre
Hope this helps :)
Using a recursive function you can find all the src
and its corresponding summed x
values.
Below is a crude example. Refactor as you see fit.
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getAllSrc(layers)
let arr = [];
layers.forEach(layer =>
if(layer.src)
arr.push(src: layer.src, x: layer.x);
else if(layer.layers)
let newArr = getAllSrc(layer.layers);
if(newArr.length > 0)
newArr.forEach((src, x) =>
arr.push(src, x: (layer.x + x));
);
);
return arr;
function getData(data)
let arr = getAllSrc(data.layers);
for (let src, x of arr)
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
Here is the CodePen for the same: https://codepen.io/anon/pen/Wmpvre
Hope this helps :)
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getAllSrc(layers)
let arr = [];
layers.forEach(layer =>
if(layer.src)
arr.push(src: layer.src, x: layer.x);
else if(layer.layers)
let newArr = getAllSrc(layer.layers);
if(newArr.length > 0)
newArr.forEach((src, x) =>
arr.push(src, x: (layer.x + x));
);
);
return arr;
function getData(data)
let arr = getAllSrc(data.layers);
for (let src, x of arr)
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function getAllSrc(layers)
let arr = [];
layers.forEach(layer =>
if(layer.src)
arr.push(src: layer.src, x: layer.x);
else if(layer.layers)
let newArr = getAllSrc(layer.layers);
if(newArr.length > 0)
newArr.forEach((src, x) =>
arr.push(src, x: (layer.x + x));
);
);
return arr;
function getData(data)
let arr = getAllSrc(data.layers);
for (let src, x of arr)
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
answered Mar 7 at 6:21
RoysonRoyson
5302514
5302514
add a comment |
add a comment |
You can do that using recursion. While going though nested obj you could parent x
value each item in layers
and check if the element in layers
have src
property add the previous x
value to it.
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function changeData(obj,x=0)
if(obj.src) obj.x += x;
if(obj.layers)
for(const item of obj.layers)
changeData(item,x+obj.x
changeData(jsonData);
function showData(obj)
if(obj.src) document.body.innerHTML += `src:$obj.src<br>
x:$obj.x<br>`;
if(obj.layers)
for(let i of obj.layers) showData(i)
showData(jsonData);
console.log(jsonData);
Thanks again for your help, As the other answer gave answer before 3 minutes, i accepted that answer....
– vickey colors
Mar 11 at 5:10
@vickeycolors Time doesn't matter in giving answer. The thing which matters is short and efficient and I think mine code is better. And if you was not to accept my answer you shouldn't have asked me that much help.well if you think his code is better so no problem
– Maheer Ali
Mar 11 at 5:26
I agree, i will rethink about it and i will take the final decision, thanks.....
– vickey colors
Mar 11 at 5:27
add a comment |
You can do that using recursion. While going though nested obj you could parent x
value each item in layers
and check if the element in layers
have src
property add the previous x
value to it.
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function changeData(obj,x=0)
if(obj.src) obj.x += x;
if(obj.layers)
for(const item of obj.layers)
changeData(item,x+obj.x
changeData(jsonData);
function showData(obj)
if(obj.src) document.body.innerHTML += `src:$obj.src<br>
x:$obj.x<br>`;
if(obj.layers)
for(let i of obj.layers) showData(i)
showData(jsonData);
console.log(jsonData);
Thanks again for your help, As the other answer gave answer before 3 minutes, i accepted that answer....
– vickey colors
Mar 11 at 5:10
@vickeycolors Time doesn't matter in giving answer. The thing which matters is short and efficient and I think mine code is better. And if you was not to accept my answer you shouldn't have asked me that much help.well if you think his code is better so no problem
– Maheer Ali
Mar 11 at 5:26
I agree, i will rethink about it and i will take the final decision, thanks.....
– vickey colors
Mar 11 at 5:27
add a comment |
You can do that using recursion. While going though nested obj you could parent x
value each item in layers
and check if the element in layers
have src
property add the previous x
value to it.
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function changeData(obj,x=0)
if(obj.src) obj.x += x;
if(obj.layers)
for(const item of obj.layers)
changeData(item,x+obj.x
changeData(jsonData);
function showData(obj)
if(obj.src) document.body.innerHTML += `src:$obj.src<br>
x:$obj.x<br>`;
if(obj.layers)
for(let i of obj.layers) showData(i)
showData(jsonData);
console.log(jsonData);
You can do that using recursion. While going though nested obj you could parent x
value each item in layers
and check if the element in layers
have src
property add the previous x
value to it.
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function changeData(obj,x=0)
if(obj.src) obj.x += x;
if(obj.layers)
for(const item of obj.layers)
changeData(item,x+obj.x
changeData(jsonData);
function showData(obj)
if(obj.src) document.body.innerHTML += `src:$obj.src<br>
x:$obj.x<br>`;
if(obj.layers)
for(let i of obj.layers) showData(i)
showData(jsonData);
console.log(jsonData);
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function changeData(obj,x=0)
if(obj.src) obj.x += x;
if(obj.layers)
for(const item of obj.layers)
changeData(item,x+obj.x
changeData(jsonData);
function showData(obj)
if(obj.src) document.body.innerHTML += `src:$obj.src<br>
x:$obj.x<br>`;
if(obj.layers)
for(let i of obj.layers) showData(i)
showData(jsonData);
console.log(jsonData);
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
function changeData(obj,x=0)
if(obj.src) obj.x += x;
if(obj.layers)
for(const item of obj.layers)
changeData(item,x+obj.x
changeData(jsonData);
function showData(obj)
if(obj.src) document.body.innerHTML += `src:$obj.src<br>
x:$obj.x<br>`;
if(obj.layers)
for(let i of obj.layers) showData(i)
showData(jsonData);
console.log(jsonData);
edited Mar 8 at 15:44
answered Mar 7 at 6:25
Maheer AliMaheer Ali
6,953518
6,953518
Thanks again for your help, As the other answer gave answer before 3 minutes, i accepted that answer....
– vickey colors
Mar 11 at 5:10
@vickeycolors Time doesn't matter in giving answer. The thing which matters is short and efficient and I think mine code is better. And if you was not to accept my answer you shouldn't have asked me that much help.well if you think his code is better so no problem
– Maheer Ali
Mar 11 at 5:26
I agree, i will rethink about it and i will take the final decision, thanks.....
– vickey colors
Mar 11 at 5:27
add a comment |
Thanks again for your help, As the other answer gave answer before 3 minutes, i accepted that answer....
– vickey colors
Mar 11 at 5:10
@vickeycolors Time doesn't matter in giving answer. The thing which matters is short and efficient and I think mine code is better. And if you was not to accept my answer you shouldn't have asked me that much help.well if you think his code is better so no problem
– Maheer Ali
Mar 11 at 5:26
I agree, i will rethink about it and i will take the final decision, thanks.....
– vickey colors
Mar 11 at 5:27
Thanks again for your help, As the other answer gave answer before 3 minutes, i accepted that answer....
– vickey colors
Mar 11 at 5:10
Thanks again for your help, As the other answer gave answer before 3 minutes, i accepted that answer....
– vickey colors
Mar 11 at 5:10
@vickeycolors Time doesn't matter in giving answer. The thing which matters is short and efficient and I think mine code is better. And if you was not to accept my answer you shouldn't have asked me that much help.well if you think his code is better so no problem
– Maheer Ali
Mar 11 at 5:26
@vickeycolors Time doesn't matter in giving answer. The thing which matters is short and efficient and I think mine code is better. And if you was not to accept my answer you shouldn't have asked me that much help.well if you think his code is better so no problem
– Maheer Ali
Mar 11 at 5:26
I agree, i will rethink about it and i will take the final decision, thanks.....
– vickey colors
Mar 11 at 5:27
I agree, i will rethink about it and i will take the final decision, thanks.....
– vickey colors
Mar 11 at 5:27
add a comment |
Here is my solution with recursion and mutation. You can cloneDeep the array before if you don't want to mutate it directly.
// Code
function recursion(obj, currentX = 0)
if(obj.src) obj.x = currentX
else if(obj.layers)
obj.layers.forEach(subObj => recursion(subObj, (currentX + subObj.x)))
// Data
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
// Use
recursion(jsonData)
console.log(jsonData)
add a comment |
Here is my solution with recursion and mutation. You can cloneDeep the array before if you don't want to mutate it directly.
// Code
function recursion(obj, currentX = 0)
if(obj.src) obj.x = currentX
else if(obj.layers)
obj.layers.forEach(subObj => recursion(subObj, (currentX + subObj.x)))
// Data
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
// Use
recursion(jsonData)
console.log(jsonData)
add a comment |
Here is my solution with recursion and mutation. You can cloneDeep the array before if you don't want to mutate it directly.
// Code
function recursion(obj, currentX = 0)
if(obj.src) obj.x = currentX
else if(obj.layers)
obj.layers.forEach(subObj => recursion(subObj, (currentX + subObj.x)))
// Data
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
// Use
recursion(jsonData)
console.log(jsonData)
Here is my solution with recursion and mutation. You can cloneDeep the array before if you don't want to mutate it directly.
// Code
function recursion(obj, currentX = 0)
if(obj.src) obj.x = currentX
else if(obj.layers)
obj.layers.forEach(subObj => recursion(subObj, (currentX + subObj.x)))
// Data
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
// Use
recursion(jsonData)
console.log(jsonData)
// Code
function recursion(obj, currentX = 0)
if(obj.src) obj.x = currentX
else if(obj.layers)
obj.layers.forEach(subObj => recursion(subObj, (currentX + subObj.x)))
// Data
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
// Use
recursion(jsonData)
console.log(jsonData)
// Code
function recursion(obj, currentX = 0)
if(obj.src) obj.x = currentX
else if(obj.layers)
obj.layers.forEach(subObj => recursion(subObj, (currentX + subObj.x)))
// Data
let jsonData =
"layers" : [
"x" : 10,
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40,
"layers" : [
"x" : 50,
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
// Use
recursion(jsonData)
console.log(jsonData)
answered Mar 7 at 7:15
birdbird
997620
997620
add a comment |
add a comment |
let jsonData =
"layers": [
"x": 10,
"layers": [
"x": 20,
"y": 30,
"name": "L2a"
,
"x": 40,
"layers": [
"x": 50,
"src": "image.png",
"y": 60,
"name": "L2b-1"
,
"x": 70,
"y": 80,
"name": "L2b-2"
],
"y": 90,
"name": "user_image_1"
,
"x": 100,
"layers": [
"x": 105,
"src": "image2.png",
"y": 110,
"name": "L2C-1"
,
"x": 120,
"y": 130,
"name": "L2C-2"
],
"y": 140,
"name": "L2"
],
"y": 150,
"name": "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
let y = layer1[0].x;
for (i = 1; i < layer2.length; i++)
var x = y;
x += layer2[i].x;
x += layer2[i].layers[0].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
add a comment |
let jsonData =
"layers": [
"x": 10,
"layers": [
"x": 20,
"y": 30,
"name": "L2a"
,
"x": 40,
"layers": [
"x": 50,
"src": "image.png",
"y": 60,
"name": "L2b-1"
,
"x": 70,
"y": 80,
"name": "L2b-2"
],
"y": 90,
"name": "user_image_1"
,
"x": 100,
"layers": [
"x": 105,
"src": "image2.png",
"y": 110,
"name": "L2C-1"
,
"x": 120,
"y": 130,
"name": "L2C-2"
],
"y": 140,
"name": "L2"
],
"y": 150,
"name": "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
let y = layer1[0].x;
for (i = 1; i < layer2.length; i++)
var x = y;
x += layer2[i].x;
x += layer2[i].layers[0].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
add a comment |
let jsonData =
"layers": [
"x": 10,
"layers": [
"x": 20,
"y": 30,
"name": "L2a"
,
"x": 40,
"layers": [
"x": 50,
"src": "image.png",
"y": 60,
"name": "L2b-1"
,
"x": 70,
"y": 80,
"name": "L2b-2"
],
"y": 90,
"name": "user_image_1"
,
"x": 100,
"layers": [
"x": 105,
"src": "image2.png",
"y": 110,
"name": "L2C-1"
,
"x": 120,
"y": 130,
"name": "L2C-2"
],
"y": 140,
"name": "L2"
],
"y": 150,
"name": "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
let y = layer1[0].x;
for (i = 1; i < layer2.length; i++)
var x = y;
x += layer2[i].x;
x += layer2[i].layers[0].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
let jsonData =
"layers": [
"x": 10,
"layers": [
"x": 20,
"y": 30,
"name": "L2a"
,
"x": 40,
"layers": [
"x": 50,
"src": "image.png",
"y": 60,
"name": "L2b-1"
,
"x": 70,
"y": 80,
"name": "L2b-2"
],
"y": 90,
"name": "user_image_1"
,
"x": 100,
"layers": [
"x": 105,
"src": "image2.png",
"y": 110,
"name": "L2C-1"
,
"x": 120,
"y": 130,
"name": "L2C-2"
],
"y": 140,
"name": "L2"
],
"y": 150,
"name": "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
let y = layer1[0].x;
for (i = 1; i < layer2.length; i++)
var x = y;
x += layer2[i].x;
x += layer2[i].layers[0].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
let jsonData =
"layers": [
"x": 10,
"layers": [
"x": 20,
"y": 30,
"name": "L2a"
,
"x": 40,
"layers": [
"x": 50,
"src": "image.png",
"y": 60,
"name": "L2b-1"
,
"x": 70,
"y": 80,
"name": "L2b-2"
],
"y": 90,
"name": "user_image_1"
,
"x": 100,
"layers": [
"x": 105,
"src": "image2.png",
"y": 110,
"name": "L2C-1"
,
"x": 120,
"y": 130,
"name": "L2C-2"
],
"y": 140,
"name": "L2"
],
"y": 150,
"name": "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
let y = layer1[0].x;
for (i = 1; i < layer2.length; i++)
var x = y;
x += layer2[i].x;
x += layer2[i].layers[0].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
let jsonData =
"layers": [
"x": 10,
"layers": [
"x": 20,
"y": 30,
"name": "L2a"
,
"x": 40,
"layers": [
"x": 50,
"src": "image.png",
"y": 60,
"name": "L2b-1"
,
"x": 70,
"y": 80,
"name": "L2b-2"
],
"y": 90,
"name": "user_image_1"
,
"x": 100,
"layers": [
"x": 105,
"src": "image2.png",
"y": 110,
"name": "L2C-1"
,
"x": 120,
"y": 130,
"name": "L2C-2"
],
"y": 140,
"name": "L2"
],
"y": 150,
"name": "L1"
]
;
function getData(data)
var dataObj = ;
let layer1 = data.layers;
let layer2 = layer1[0].layers;
let y = layer1[0].x;
for (i = 1; i < layer2.length; i++)
var x = y;
x += layer2[i].x;
x += layer2[i].layers[0].x;
var src = layer2[i].layers[0].src;
document.write("src :" + src);
document.write("<br>");
document.write("x:" + x);
document.write("<br>");
getData(jsonData);
answered Mar 7 at 13:28
Syed Mehtab HassanSyed Mehtab Hassan
388115
388115
add a comment |
add a comment |
Here is My Solution. Check this out
let jsonData =
"layers" : [
"x" : 10, //
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40, //
"layers" : [
"x" : 50, //
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
var dataObj = [];
var x, src;
function getData(data)
let layer1 = data.layers;
for(var i = 0; i < layer1.length; i++)
if(x === 0)
x = layer1[i].x;
continue;
x = layer1[i].x;
if(typeof layer1[i].layers !== 'undefined')
createOutput(layer1[i].layers);
return dataObj;
function createOutput(dataArray)
if(typeof dataArray === 'undefined')
dataObj.push(x: x, src: src);
x = 0;
getData(jsonData);
return;
dataArray.forEach(element => typeof element.src !== 'undefined')
x += element.x;
src = element.src;
createOutput(element.layers);
)
console.log(JSON.stringify(getData(jsonData)));
add a comment |
Here is My Solution. Check this out
let jsonData =
"layers" : [
"x" : 10, //
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40, //
"layers" : [
"x" : 50, //
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
var dataObj = [];
var x, src;
function getData(data)
let layer1 = data.layers;
for(var i = 0; i < layer1.length; i++)
if(x === 0)
x = layer1[i].x;
continue;
x = layer1[i].x;
if(typeof layer1[i].layers !== 'undefined')
createOutput(layer1[i].layers);
return dataObj;
function createOutput(dataArray)
if(typeof dataArray === 'undefined')
dataObj.push(x: x, src: src);
x = 0;
getData(jsonData);
return;
dataArray.forEach(element => typeof element.src !== 'undefined')
x += element.x;
src = element.src;
createOutput(element.layers);
)
console.log(JSON.stringify(getData(jsonData)));
add a comment |
Here is My Solution. Check this out
let jsonData =
"layers" : [
"x" : 10, //
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40, //
"layers" : [
"x" : 50, //
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
var dataObj = [];
var x, src;
function getData(data)
let layer1 = data.layers;
for(var i = 0; i < layer1.length; i++)
if(x === 0)
x = layer1[i].x;
continue;
x = layer1[i].x;
if(typeof layer1[i].layers !== 'undefined')
createOutput(layer1[i].layers);
return dataObj;
function createOutput(dataArray)
if(typeof dataArray === 'undefined')
dataObj.push(x: x, src: src);
x = 0;
getData(jsonData);
return;
dataArray.forEach(element => typeof element.src !== 'undefined')
x += element.x;
src = element.src;
createOutput(element.layers);
)
console.log(JSON.stringify(getData(jsonData)));
Here is My Solution. Check this out
let jsonData =
"layers" : [
"x" : 10, //
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40, //
"layers" : [
"x" : 50, //
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
var dataObj = [];
var x, src;
function getData(data)
let layer1 = data.layers;
for(var i = 0; i < layer1.length; i++)
if(x === 0)
x = layer1[i].x;
continue;
x = layer1[i].x;
if(typeof layer1[i].layers !== 'undefined')
createOutput(layer1[i].layers);
return dataObj;
function createOutput(dataArray)
if(typeof dataArray === 'undefined')
dataObj.push(x: x, src: src);
x = 0;
getData(jsonData);
return;
dataArray.forEach(element => typeof element.src !== 'undefined')
x += element.x;
src = element.src;
createOutput(element.layers);
)
console.log(JSON.stringify(getData(jsonData)));
let jsonData =
"layers" : [
"x" : 10, //
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40, //
"layers" : [
"x" : 50, //
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
var dataObj = [];
var x, src;
function getData(data)
let layer1 = data.layers;
for(var i = 0; i < layer1.length; i++)
if(x === 0)
x = layer1[i].x;
continue;
x = layer1[i].x;
if(typeof layer1[i].layers !== 'undefined')
createOutput(layer1[i].layers);
return dataObj;
function createOutput(dataArray)
if(typeof dataArray === 'undefined')
dataObj.push(x: x, src: src);
x = 0;
getData(jsonData);
return;
dataArray.forEach(element => typeof element.src !== 'undefined')
x += element.x;
src = element.src;
createOutput(element.layers);
)
console.log(JSON.stringify(getData(jsonData)));
let jsonData =
"layers" : [
"x" : 10, //
"layers" : [
"x" : 20,
"y" : 30,
"name" : "L2a"
,
"x" : 40, //
"layers" : [
"x" : 50, //
"src" : "image.png",
"y" : 60,
"name" : "L2b-1"
,
"x" : 70,
"y" : 80,
"name" : "L2b-2"
],
"y" : 90,
"name" : "user_image_1"
,
"x" : 100,
"layers" : [
"x" : 105,
"src" : "image2.png",
"y" : 110,
"name" : "L2C-1"
,
"x" : 120,
"y" : 130,
"name" : "L2C-2"
],
"y" : 140,
"name" : "L2"
],
"y" : 150,
"name" : "L1"
]
;
var dataObj = [];
var x, src;
function getData(data)
let layer1 = data.layers;
for(var i = 0; i < layer1.length; i++)
if(x === 0)
x = layer1[i].x;
continue;
x = layer1[i].x;
if(typeof layer1[i].layers !== 'undefined')
createOutput(layer1[i].layers);
return dataObj;
function createOutput(dataArray)
if(typeof dataArray === 'undefined')
dataObj.push(x: x, src: src);
x = 0;
getData(jsonData);
return;
dataArray.forEach(element => typeof element.src !== 'undefined')
x += element.x;
src = element.src;
createOutput(element.layers);
)
console.log(JSON.stringify(getData(jsonData)));
answered Mar 15 at 4:42
Darshana PathumDarshana Pathum
984
984
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54995969%2fadd-the-x-values-in-json-with-respect-to-image%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown