Title: Javascript array change value recursively
Tags: javascript;arrays;recursion
Question: I'm stucking at changing an value of an array with unknown depth.
I want to iterate over it and change specific values, after all changes are done I want to receive the whole array back with the applied changes.
Code sample:
var array = [];
array.name = "Page";
array.inside = [];
array.inside.name = "Page inside";
array.inside.inside = [];
array.inside.inside.name = "Page inside inside";
array.inside.inside.inside = [];
function changeValue(array) {
for (var ii in array) {
if (typeof array[ii] === 'object') changeValue(array[ii]);
if (array.name) {
array.name = array.name + " changed";
}
console.log(array);
changeValue(array);
Comment: Here you have managed to change all values. What specific value do you want to change? You also do understand that you are actually not using an array. You are using basic JavaScript objects. So your `array` is an object with properties `name` and `inside`. `inside` then is another object.


Here is another answer: Try this:
var array = [];
array.name = "Page";
array.inside = [];
array.inside.name = "Page inside";
array.inside.inside = [];
array.inside.inside.name = "Page inside inside";
array.inside.inside.inside = [];
function changeValue(array) {
if (array.name) {
array.name = array.name + " changed";
console.log(array);
for (var ii in array) {
if (typeof array[ii] === 'object') changeValue(array[ii]);
changeValue(array);
Every time through the loop you are changing array.name.  That is why you're results have ' changed changed'.  Take the array.name modification out of the loop and it works just as expected.
Good luck.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:01/25/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><arrays><recursion>', 'Title': 'Javascript array change value recursively', 'ViewCount': 1723, 'id': 28139962, 'user_AboutMe': '', 'user_AccountId': 5204525.0, 'user_DisplayName': 'Xilef', 'user_Reputation': 29.0, 'user_Views': 9.0, 'user_id': 4162910.0}}