Change nested object data using javascript

I would like to touch on all elements of the array. The end result will be decodeURIcomponentfor each item.

It does not work for me, so I decided to change the value of each element to check the code. This does not change anything.

Is there a better way to do this, since the really nested functions $.eachseem redundant to me.

$(function() {

var hResponse = [];
hResponse.push("firstName", "lastName", "location");

var columns = [];
columns.push({
    "firstName": "Edward",
        "lastName": "Dane",
        "location": " Here"
}, {
    "firstName": "Arthur",
        "lastName": "Dee",
        "location": "There"
}, {
    "firstName": "Cherry",
        "lastName": "Red",
        "location": "OverHere"
});
$.each(columns, function (key, value) {

    $.each(value, function (key1, value2) {

        value2 = "Meeeeep";
        //value2 = decodeURIComponent(value2);
    });

});


$('#my-table').dynatable({
    dataset: {
        records: columns
    }
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="https://s3.amazonaws.com/dynatable-docs-assets/css/jquery.dynatable.css" rel="stylesheet"/>
<script src="https://s3.amazonaws.com/dynatable-docs-assets/js/jquery.dynatable.js"></script>

<table id="my-table">
    <thead>
        <th>FirstName</th>
        <th>LastName</th>
    </thead>
    <tbody></tbody>
</table>
<br>
Run codeHide result
+4
source share
1 answer

JavaScript is pass-by-value . Assigning a value to a variable does not change the value of another variable or property (other than global and with). You must return the changed value of the property of the object.

:

$.each(columns, function (key, value) {
    $.each(value, function (key1, value2) {
        value[key] = decodeURIComponent(value2);
    });
});
+5

All Articles