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>

3
  • 2
    columns[key][key1] = "Meeeep"; Commented Mar 26, 2015 at 19:35
  • 1
    Sorry for being a pedant, but that's not JSON, it's just a javascript object Commented Mar 26, 2015 at 19:35
  • 1
    jsfiddle.net/oahypr9v/1 Assign value to the object Commented Mar 26, 2015 at 19:35

1 Answer 1

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);
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

columns[key] === value, and value is an object. Either way works.
Maybe it would be more appropriately called call by sharing?
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?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.