我所需要的
阵列结构
Array
(
[0] => Array
(
[id] => 50615
[des] => PHARMA Pro&Pack Expo 2015 is organized by IPMMA and will be held at Mumbai, the economic capital of India. This show helps the exhibitors from all ove
[membership] => 0
[name] => Pharma Pro&Pack Expo
[abbr_name] => Pharma Pro&Pack Expo
[paid] => 0
[event_wrapper] => 33587
[event_samll_wrapper] => http://im.gifbt.com/industry/27-350x210.jpg
[event_url] => pharma-propack-expo
[website] => http://www.pharmapropack.com
[eventType] => 1
[venue_name] => Bombay Convention & Exhibition Centre (BCEC)
[startDate] => 2015-05-13
[endDate] => 2015-05-15
[city] => Mumbai
[country] => India
[country_url] => india
[country_shortname] => India
[industry_id] => 27
[industry_name] => Medical & Pharmaceutical
[industry_url] => medical-pharma
[event_status] =>
[total_visitors] => 144
[total_exhibitor] => 0
[total_speakers] => 0
)
[1] => Array
(
[id] => 57271
[des] => The Iphex is a well acknowledged event in the pharmaceutical and healthcare industry and with the presence of pharmaceutical products and equipments,
[membership] => 0
[name] => Iphex
[abbr_name] => Iphex
[paid] => 0
[event_wrapper] => 41539
[event_samll_wrapper] => http://im.gifbt.com/industry/27-350x210.jpg
[event_url] => iphex
[website] => http://iphex-india.com/
[eventType] => 1
[venue_name] => Bombay Exhibition Centre
[startDate] => 2015-05-13
[endDate] => 2015-05-15
[city] => Mumbai
[country] => India
[country_url] => india
[country_shortname] => India
[industry_id] => 27
[industry_name] => Medical & Pharmaceutical
[industry_url] => medical-pharma
[event_status] =>
[total_visitors] => 134
[total_exhibitor] => 120
[total_speakers] => 0
)
[2] => Array
(
[id] => 175534
[des] => The TENSYMP, organized by the CimlGlobal will take place from 13th May to the 15th May 2015 at the Courtyard by Marriott, India in Ahmedabad, India. T
[membership] =>
[name] => TENSYMP Ahmedabad
[abbr_name] => TENSYMP Ahmedabad
[paid] =>
[event_wrapper] =>
[event_samll_wrapper] => http://im.gifbt.com/industry/40-350x210.jpg
[event_url] => tensymp-ahmedabad
[website] => http://www.tensymp2015.org/
[eventType] => 2
[venue_name] => Gujarat International Finance Tec-City
[startDate] => 2015-05-13
[endDate] => 2015-05-15
[city] => Ahmedabad
[country] => India
[country_url] => india
[country_shortname] => India
[industry_id] => 40
[industry_name] => Scientific Instruments
[industry_url] => scientific-instruments
[event_status] =>
[total_visitors] => 3
[total_exhibitor] => 0
[total_speakers] => 3
)js码
var desktop="/app.php/notificationdetail";
var http = new XMLHttpRequest
url = desktop,
params = url;
http.open("POST", url, !0), http.setRequestHeader("Content-type", "application/json; charset=UTF-8"), http.setRequestHeader("Content-length", params.length), http.setRequestHeader("Connection", "close"),
http.onreadystatechange = function() {
if (4 == http.readyState && 200 == http.status) {
var obj=http.responseText;
alert(typeof obj);//string
var parsed = JSON.parse(obj);
for (var c in obj) {
var newElement = document.createElement('div');
newElement.id = obj[c]; newElement.className = "notification";
newElement.innerHTML = obj[c];
document.body.appendChild(newElement);
}
}
}, http.send(params);问题
交货期应该是
我试过使用for循环
for (var x=0; x<obj.length; x++)
{
var name= obj[x].name;
console.log(name);//outputs : undefined
}发布于 2015-05-14 06:05:17
获取行数(对象计数)和列数(字段计数)的:
不熟悉w/ php Array,但在我看来,它是一个一维数组,每个数组元素都具有HashTable结构。
只是确认在您的console.log(parsed);行之后添加var parsed = JSON.parse(obj);。你应该看到类似[{id:50615,des:'PHARMA...',...},{id:57271,des:'The Iphex...',...},{...}]的东西。
如果是这样的话,parsed.length应该给出数据结构中对象(行)的数量,而Object.keys(parsed[0]).length将给出对象中的列/字段的数量。
注意__:这里唯一的事情是,我假设数组中的所有对象都有相同数量的字段。如果不是,您可能必须循环遍历数组,并计算数组中每个对象的最大长度,并将其用作列计数。
显示特定字段的:
一旦了解了对象数组的布局方式,就可以使用Array.map函数从原始数组中提取某些字段,如下所示:
var model = parsed.map(function(o) { return { id: o.id, name: o.name, city: o.city }; });
并使用现在应该只包含一个具有id、名称和city字段的对象数组的模型。
您似乎已经知道如何做DOM操作了,所以我不会去那里。我唯一能给出的建议是要么使用字符串连接并将其大容量注入DOM元素innerHTML,要么使用documentFragment方法来提高性能。请参阅:http://jsperf.com/concat-vs-docfrag/4
https://stackoverflow.com/questions/30229893
复制相似问题