0

Hello I have a JQuery/Ajax function as following:

$.ajax({
    type : "POST",
    url  : "/posts/getids", 
    success: function(response){
        console.log(response);                                                                  
    },
    error: function() {
        alert('An unexpected error has occurred! Please try later.');
    }
});

In my cakePHP script I'm sending an array using the json_encode($array) function.

In firebug I get this result :

[{"Post":{"id":1}},{"Post":{"id":2}},{"Post":{"id":4}},{"Post":{"id":3}}]

So my question is How can I simply print only the ids like this : 1, 2, 3, 4

Thank you.

1 Answer 1

1
// Convert JSON to JavaScript array.
var dataFromServer = JSON.parse(response);

// Creating an array of id's.
var idArray = [];

// Moving data to "idArray".
for(i = 0; i < dataFromServer.length; i++){
    idArray[i] = dataFromServer[i].Post.id;
}

// Checking the result.
console.log(idArray);

// [1, 2, 3, 4].
Sign up to request clarification or add additional context in comments.

Comments

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.