使用javascript更改嵌套对象数据

我想影响数组中的所有项目.最终结果将是每个项目上的decodeURIcomponent.

它不适合我,所以我决定更改每个项目的值来检查代码.它没有改变任何东西.

有没有更好的方法来实现这一点,因为真正嵌套的$.each函数对我来说似乎有点多余.

$(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>
最佳答案
JavaScript全部为pass-by-value.为变量赋值不会更改另一个变量或属性的值(全局和范围除外).您必须将更改的值分配回对象属性.

只需对代码进行最少的更改:

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

转载注明原文:使用javascript更改嵌套对象数据 - 代码日志