简体   繁体   English

javasctipt / jquery数组解析

[英]javasctipt/jquery array parsing

I was looking for a picklist template in javascript and came across this code. 我一直在用javascript寻找选项列表模板,并且遇到了这段代码。

JsFiddle for picklist JsFiddle的选择列表

Which has the following code to parse through a from what i am assuming a Jquery object array. 其中具有以下代码来解析我所假设的Jquery对象数组。

<script>

     var val = {
        01: {id: 01, text: 'test2'},
        02: {id: 02, text: 'test3'},
        03: {id: 03, text: 'test2'},
     };

     var pick = $("#pickList").pickList({data: val});

     $("#getSelected").click(function () {
        console.log(pick.getValues(val));
        alert(pick.getValues());
     });
     </script>

Where the #pickList is a function which populates data onto a form. #pickList是将数据填充到表单上的函数。

Now when I run this using the button "#getSelected" , I can see in chrome developer options that it is fetching the data from the form but in some sort of an key value pair. 现在,当我使用按钮“ #getSelected”运行它时,我可以在chrome开发人员选项中看到它正在从表单中获取数据,但是是以某种键值对的形式。

Now, When I try to alert(pick.getValues()); 现在,当我尝试alert(pick.getValues());

I get an alert with [object Object] and not the value inside the array. 我收到[object Object]而不是数组中的值的警报。

Can someone help me to know how to parse the array so that i can get the value of the "text" 有人可以帮助我知道如何解析数组,以便我可以获取“文本”的值

Below is a screenshot of the Chrome dev console. 以下是Chrome开发者控制台的屏幕截图。 Screenshot of chrome dev options chrome dev选项的屏幕截图

You need to use JSON.stringify inside your alert statement like this, 您需要像这样在alert语句中使用JSON.stringify

alert(JSON.stringify(pick.getValues()));

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM