4

I'd like to affect all items in the array. The end result will be to decodeURIcomponent on each item.

It's not working for me so I decided to change the value of each item to check the code. It doesn't change anything.

Is there a better way of doing this, as really nested $.each functions seems a bit 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>
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
platinums
  • 634
  • 1
  • 10
  • 21

1 Answers1

5

JavaScript is all pass-by-value. Assigning a value to a variable does not change the value of another variable or property (except in global and with scope). You have to assign the changed value back to the object property.

With minimal changes to your code:

$.each(columns, function (key, value) {
    $.each(value, function (key1, value2) {
        value[key] = decodeURIComponent(value2);
    });
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 1
    `columns[key] === value`, and `value` is an object. Either way works. – Felix Kling Mar 26 '15 at 19:37
  • Maybe it would be more appropriately called [call by sharing](http://stackoverflow.com/a/3638034/1896761)? – Austin Mullins Mar 26 '15 at 19:45
  • thanks so much, i usually rtfm, but i couldn't find any guides on accessing the object, specifically the [key] part , where can i find more info on this? – platinums Mar 26 '15 at 20:02
  • @Austin: yeah, but that term is not really used... I'm also not sure whether it's a good idea conflate strategies of passing data with strategies of representing data. – Felix Kling Mar 26 '15 at 20:05
  • @platinums: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects, http://stackoverflow.com/q/11922383/218196 – Felix Kling Mar 26 '15 at 20:07