首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在javascript中获取2d数组的长度,并在js创建的动态div中显示数据

如何在javascript中获取2d数组的长度,并在js创建的动态div中显示数据
EN

Stack Overflow用户
提问于 2015-05-14 05:29:12
回答 1查看 1.8K关注 0票数 0

我所需要的

  • 我需要在图标上显示数字。
  • 我需要使用使用javascript的数组从输入中生成动态div。

阵列结构

代码语言:javascript
复制
            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码

代码语言:javascript
复制
      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);

问题

  • 我需要找到数组的长度,比如如果使用php计数,那么它将显示2个数组,而如果在js中使用.length(),则不会返回所需的结果。

交货期应该是

  • 通知中的计数应该是2使用javascript。
  • 我需要在动态div中显示“id”、“name”、“city”。

我试过使用for循环

代码语言:javascript
复制
for (var x=0; x<obj.length; x++)
 {
   var name= obj[x].name;
    console.log(name);//outputs : undefined
 }
EN

回答 1

Stack Overflow用户

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30229893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档