如何打印jquery对象/数组

时间:2022-06-27 02:37:20

I get id and category name from mysql database.

我从mysql数据库中获取id和类别名称。

When I am alerting a result, I get:

当我提醒结果时,我得到:

[{"id":"197","category":"Damskie"},"id":"198","category":"M\u0119skie"}]

(Is this object?)

(这个对象?)

  1. How can I print a result like this:

    如何打印如下结果:

    Damskie

    Damskie

    M\u0119skie

    中号\ u0119skie

  2. M\u0119ski - has bad encoding. It should be Męskie. How can I change this?

    M \ u0119ski - 编码错误。它应该是Męskie。我怎么能改变这个?

4 个解决方案

#1


24  

var arrofobject = [{"id":"197","category":"Damskie"},{"id":"198","category":"M\u0119skie"}];

$.each(arrofobject, function(index, val) {
    console.log(val.category);
});

#2


6  

What you have from the server is a string like below:

你从服务器得到的是一个如下字符串:

var data = '[{"id":"197","category":"Damskie"},{"id":"198","category":"M\u0119skie"}]';

Then you can use JSON.parse function to change it to an object. Then you access the category like below:

然后,您可以使用JSON.parse函数将其更改为对象。然后您访问如下所示的类别:

var dataObj = JSON.parse(data);

console.log(dataObj[0].category); //will return Damskie
console.log(dataObj[1].category); //will return Męskie

#3


2  

Your result is currently in string format, you need to parse it as json.

您的结果当前是字符串格式,您需要将其解析为json。

var obj = $.parseJSON(result);
alert(obj[0].category);

Additionally, if you set the dataType of the ajax call you are making to json, you can skip the $.parseJSON() step.

另外,如果你设置了json调用的ajax调用的dataType,你可以跳过$ .parseJSON()步骤。

#4


0  

var arrofobject = [{"id":"197","category":"Damskie"},{"id":"198","category":"M\u0119skie"}];
var data = arrofobject.map(arrofobject => arrofobject);
console.log(data)

for more details please look at jQuery.map()

有关更多详细信息,请查看jQuery.map()

#1


24  

var arrofobject = [{"id":"197","category":"Damskie"},{"id":"198","category":"M\u0119skie"}];

$.each(arrofobject, function(index, val) {
    console.log(val.category);
});

#2


6  

What you have from the server is a string like below:

你从服务器得到的是一个如下字符串:

var data = '[{"id":"197","category":"Damskie"},{"id":"198","category":"M\u0119skie"}]';

Then you can use JSON.parse function to change it to an object. Then you access the category like below:

然后,您可以使用JSON.parse函数将其更改为对象。然后您访问如下所示的类别:

var dataObj = JSON.parse(data);

console.log(dataObj[0].category); //will return Damskie
console.log(dataObj[1].category); //will return Męskie

#3


2  

Your result is currently in string format, you need to parse it as json.

您的结果当前是字符串格式,您需要将其解析为json。

var obj = $.parseJSON(result);
alert(obj[0].category);

Additionally, if you set the dataType of the ajax call you are making to json, you can skip the $.parseJSON() step.

另外,如果你设置了json调用的ajax调用的dataType,你可以跳过$ .parseJSON()步骤。

#4


0  

var arrofobject = [{"id":"197","category":"Damskie"},{"id":"198","category":"M\u0119skie"}];
var data = arrofobject.map(arrofobject => arrofobject);
console.log(data)

for more details please look at jQuery.map()

有关更多详细信息,请查看jQuery.map()