1

I have an array like this containing data like : [mode:id:A:B]

eg:

["del:123456789:0:0", "del:99887766:0:0", "edit:1471872633890:8845:0"]

is there anyway I can check and see if there is a partial match on the id and the remove then entry and replace it ?

eg:

match on 1471872633890 and replace the entire entry match with edit:1471872633890:8845:NE so the array becomes :

["del:123456789:0:0", "del:99887766:0:0", "edit:1471872633890:8845:NW"]

As an aside... can you serialize an array for ajax posting ? If not I need to rethink this anyway !

Thanks

UPDATE I'm trying to update an existing array entry or add a new entry.

This is what I've got...

    var id = $(this).closest('tr').find('input[name="id[]"]').val()
    var A = $(this).closest('tr').find('input[name="A[]"]').val()
    var B = $(this).closest('tr').find('input[name="A[]"]').val()
    var res = 'edit:' + id + ':' + A + ':' + B;

    filters = filters.map(function(value) {
    if( value.indexOf(id) > -1 ) {
        return res;
    }
        return value;
    });

How do I actually update the values in the array ?

1
  • Please include attempted solutions, why they didn't work, and the expected results. That would really helps us to figure out the issue with your code. Thanks! Commented Aug 22, 2016 at 14:06

1 Answer 1

4

I would use map in this case. You can even use another loop, but it is easy to handle in this case and I like the behavior. Just do a search inside and return the new value.

var arr = ["del:123456789:0:0", "del:99887766:0:0", "edit:1471872633890:8845:0"];

arr = arr.map(function(value) {
    if( value.indexOf("1471872633890") > -1 ) {
        return "edit:1471872633890:8845:NW";
    }
  
    return value;
});

console.log(arr);

Or if you like, as for loop:

var arr = ["del:123456789:0:0", "del:99887766:0:0", "edit:1471872633890:8845:0"];

for( var i = 0; i < arr.length; i++ ) {
    if( arr[i].indexOf("1471872633890") > -1 ) {
        arr[i] = "edit:1471872633890:8845:NW";
    }
}

console.log(arr);

If you use jQuery ajax there is no need to serialize the array. You can just send them with the data property.

var arr = ["del:123456789:0:0", "del:99887766:0:0", "edit:1471872633890:8845:0"];

$.ajax({
    url: "foo.php",
    method: "post",
    data: {
        myArray: arr
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for this.. I'm trying to use map but I always get an empty array. I've updated my question with some further details.
An empty array? Well, that is nearly impossible with your code. Please edit your question and add the full code. There is something else wrong. @Tom
Hi - I've create a codepen here: http://s.codepen.io/Tom_T/debug/jAoyvv I know what the issue is, but not how to resolve it. On start up the array is empty. When you edit a field the array is populated. If you edit again then the array needs to be updated.. The issue is due to me not pushing the value to the array if it doesn't need to be edited. ie a new entry.. I'm not sure how to do that !
I've got this working. following the filters.map I'm checking if the entry exists in the array and then pushing it in if it doesn't.. Seems to do what I need.

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.