0

after I send an $.ajax request I get a response which looks like this:

enter image description here

(don't be afraid of Russian symbols, they are just strings of text:))

Actually, I know that when php send the request, it was a simple array like:

0 => array(2) ('hotel'=>'Отельчик', city_from => 'Москва'),
1 => array(6) ('birth_date'=>"2999-01-01", 'doc_fullnumber'=> '1', 'is_buyer'=>'0', 'is_tourist'=>'0', 'lastName'=>'Имя', name => 'Имя')

and so on....

It seems much easier for me to process arrays, not objects in JS. Could anyone give an advice of how to turn this object into simple array without all those '_ proto _: Object ' and all other Jquery specific stuff?

3 Answers 3

2

I think you need this

var finalArray = [];
for(var index in response){
    toPush = [];
    for(var key in response[index]{
        toPush.push(response[index][key]);
    }
    finalArray.push(toPush);
}

This will remap your response in an array of arrays. Hope I understood right. Even I must say it's more convenient to process objects, than arrays.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Frank. It outputs: joxi.ru/MAj0z5yI4Ovxam It still has all those proto:Array(0) and stuff. Or is this normal Jquery behaviour? If so, I'll close the question. And maybe it's easier to operate objects, I am a newbee:)
It's normal behaviour!
Protos are functions available for entities of certain types...for example when you have var pippo = [1], you can call some functions from the array itself pippo.push(2)... Same thing for objects, strings..ecc...
1

Hey you can do one simple trick.

When you are sending data from your Php do like this:

$data_to_send = array("which will have your data");

echo json_decode(json_encode($data_to_send),true);

This will convert your object to array and you can use it like that in JavaScript too.

2 Comments

Yea... @SergejFomin You may accept my answer as Best answer. (That green tick).
Thank you! would use your approach and others and then choose the best answer:)
1

From Object.prototype.proto:

The __proto__ property of Object.prototype is an accessor property (a getter function and a setter function) that exposes the internal [[Prototype]] (either an object or null) of the object through which it is accessed.

Warning: Changing the [[Prototype]] of an object is, by the nature of how modern JavaScript engines optimize property accesses, a very slow operation, in every browser and JavaScript engine. The effects on performance of altering inheritance are subtle and far-flung, and are not limited to simply the time spent in obj.__proto__ = ... statement, but may extend to any code that has access to any object whose [[Prototype]] has been altered. If you care about performance you should avoid setting the [[Prototype]] of an object. Instead, create a new object with the desired [[Prototype]] using Object.create().

You already have an array of objects. You can iterate over the array of objects by this way:

var data = [{
  "hotel": "Гостиница",
  "city_from": "Mockba"
}, {
  "birth_date": "2999-01-01",
  "doc_fullnumber": 1,
  "is_buyer": 0,
  "is_tourist": 0,
  "lastName": "LastName1",
  "name": "Name1"
}, {
  "birth_date": "2999-01-02",
  "doc_fullnumber": 1,
  "is_buyer": 0,
  "is_tourist": 0,
  "lastName": "LastName2",
  "name": "Name2"
}, {
  "birth_date": "2999-01-01",
  "doc_fullnumber": 1,
  "is_buyer": 0,
  "is_tourist": 0,
  "lastName": "LastName3",
  "name": "Name3"
}];

function showData(data) {
  console.log(data);
  var i, len = data.length, html = "<ul>", obj;
  for (i = 0; i < len; i++) {
    obj = data[i];
    html += "<li>";
    html += obj.name !== undefined ? obj.name : "";
    html += " ";
    html += obj.lastName !== undefined ? obj.lastName : "";
    html += "</li>";
  }
  html += "</ul>";
  return html;
}
document.getElementById("data").innerHTML = showData(data);
<div id="data"></div>

enter image description here

Remember: MDN - Details of the object model

JavaScript is an object-based language based on prototypes, rather than being class-based. Because of this different basis, it can be less apparent how JavaScript allows you to create hierarchies of objects and to have inheritance of properties and their values.

4 Comments

Thank you, man!:) seems you know Russian? :) Yeah, the main thing is how to process it. I would use your approach and others and then choose the best answer:)
You're welcome dude. Let me know if you need more help. Удачи! :)
Hi, Danny! Actually, your answer helped me most. But I've chosen another answer as mathing better the actual question in the topic. Sorry:) And thank you again!
:) maybe you can help me with this also: stackoverflow.com/questions/44886845/…

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.