1

I have a JSON string array of objects like this.

[{"id":"4","rank":"adm","title":"title 1"},
{"id":"2","rank":"mod","title":"title 2"},
{"id":"5","rank":"das","title":"title 3"},
{"id":"1","rank":"usr","title":"title 4"},
{"id":"3","rank":"ref","title":"title 5"}]

I want to change the title value of it, once the id is matching. So if my variable myID is 5, I want to change the title "title 5" to new title, and so on. And then I get the new JSON array to $("#rangArray").val(jsonStr);

Something like

$.each(jsonStr, function(k,v) {
    if (v==myID) {
        this.title='new title'; $("#myTextArea").val(jsonStr);
    }
});

Here is the full code.

$('img.delete').click(function() {
var deltid = $(this).attr("id").split('_');
var newID = deltid[1];
var jsonStr = JSON.stringify(myArray);
$.each(jsonStr, function(k,v) {
if (v==newID) {
// how to change the title
jsonStr[k].title = 'new title';
alert(jsonStr);
$("#rangArray").val(jsonStr);
}
});
});

The above is not working. Any help please?

2
  • You have a syntax error in your array literal: you have two commas ,, in row two. Commented Feb 17, 2011 at 14:48
  • Can OP change the title to avoid misleading other users? Commented May 27, 2016 at 13:30

3 Answers 3

1

Try this:

$.each(jsonStr, function(i,v) {
    if ( v.id == myID ) {
        v.title = 'new title';
        return false;
    }
});

Live demo: http://jsfiddle.net/CVvW4/1/


Full snipped (with my code implemented):

$('img.delete').click(function() {
    var newID = $(this).attr('id').split('_')[1];

    $.each(myArray, function(i,v) {
        if ( v.id == newID ) {
            v.title = 'new title';
            $('#rangArray').val(jsonStr);
        }
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Sime, but here > if ( jsonStr[k].id == v ) { I need to compare the value of v with a previously defined variable. What do you think.
@Šime Vidas Many many thanks. I really appreciate for your help (and sorry for the delayed reply, I had to go out for some work suddenly). It's good, but I have this issue of "undefined". In the line if (v.id == newID) { I am getting the value of v.id as undefined, and hence the whole if condition is not getting executed. Any idea on this?
@Jeremy I've updated my code. Try now. (I've passed the myArray variable into the $.each method directly.)
@Šime Vidas Cool, and this solves the problem wonderfully. Many many thanks once again, Sime. :)
0

You should be doing jsonStr[v].title = "new title";

Your jsonStr is an array of objects.

Comments

0

Try using jsonStr[k].title = 'new title';

1 Comment

Thanks for the new one, but it's still not working. or probably, it's working but I'm not able to see it, in #myTextArea or even using an alert just after jsonStr[k].title = 'new title'; alert(jsonStr);. I have even tried jsonStr[2].title = 'new title'; (replacing k with 2, just to check if that changes the 3rd element of the array), it's not.

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.