Tag: 数组

jQuery JSON解码(PHP到Javascript)

我正在尝试制作一个自动完成脚本。 我通过JSON传递变量,然后我不知道如何继续解码JSON。 这是我得到的JSON代码的一个例子,我想在一个简单的javascript数组中转换它: [{“ID”:”1″,”name”:”Amateur astronomy \r”},{“ID”:”2″,”name”:”Amateur microscopy \r”},{“ID”:”173″,”name”:”Amateur radio \r”},{“ID”:”299″,”name”:”Amateur astronomy \r”},{“ID”:”349″,”name”:”Amateur theater \r”}]

Localstorage&JSON:我如何只删除一个键中的一个数组,因为localstorage.removeItem需要整个键

我在localStorage中有这个: [{“id”:”item-1″,”href”:”google.com”,”icon”:”google.com”}, {“id”:”item-2″,”href”:”youtube.com”,”icon”:”youtube.com”}, {“id”:”item-3″,”href”:”google.com”,”icon”:”google.com”}, {“id”:”item-4″,”href”:”google.com”,”icon”:”google.com”}, {“id”:”item-5″,”href”:”youtube.com”,”icon”:”youtube.com”}, {“id”:”item-6″,”href”:”asos.com”,”icon”:”asos.com”}, {“id”:”item-7″,”href”:”google.com”,”icon”:”google.com”}, {“id”:”item-8″,”href”:”mcdonalds.com”,”icon”:”mcdonalds.com”}] 当localstorage.removeItem需要整个密钥时,如何只删除id:item-3 ? 我使用此方法更新数组中的特定值: http : //jsfiddle.net/Qmm9g/所以使用相同的方法我想删除特定的数组。 请注意,已有一个要删除的按钮。 该按钮我想要一个function,它将删除整个数组( {“id”:”item-3″,”href”:”google.com”,”icon”:”google.com”} ) ID:item-3

使用jQuery对DOM元素进行排序以排序数组

刚才我问了这个问题: javascript sort数组与订单数组一致,基于什么原因是一个错误的前提。 (我已经太久了,请给我rest一下)。 我真正需要做的是对DOM元素进行排序。 性能是我的应用程序的关键 ,因为它将在CPU非常糟糕的情况下运行。 所以我拥有的是这样的: Jack Jill Nancy Tom Cartman 在JavaScript中: $modules = $(‘#moduleWrap .module’); order = [3,1,4,0,2]; 我需要的是这个: Tom Jill Cartman Jack Nancy 我(错误地)认为我可以对jQuery对象进行排序并简单地附加结果。 事实并非如此。

如何在jquery中找到2个数组之间的公共元素

var array1 = [1, 2, 3, 4, 5, 6]; var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 我有两个像上面这样的数组。 现在我想在MVC 4中使用jQuery执行以下操作。 如果两个数组的每个元素都相等,则显示消息/警报。 例如“所有记录已经存在。” 如果两个数组的每个元素都不同,那么只需将它们全部添加到“VAR”中,例如var resultset = …. (其中将存储7,8,9) 如果两个数组之间通用的元素很少,那么公共元素显示带元素的消息,例如“记录1,2,3,4,5,6已经存在”并在“VAR”中添加不同的元素,例如var resultset = …. (将存储7,8,9)。 消息和差异元素集合都将同时执行。

如何使用Codeigniter和Jqueryvalidation多维数组

嗨,我需要validation这样的多维表单 <input type="text" class="input-xlarge span5 req" id="contact_first_name" name="hotel[][contact_first_name]” value=””> <input type="text" class="input-xlarge span5 req" id="contact_last_name" name="hotel[][contact_last_name]” value=””> 我不知道最终数组的维度,因为输入是通过jquery动态添加的。 我正在使用Codeigniter Form_Validation用于服务器端,并通过JQuery使用JQuery Validator用于客户端。 这是我的form_validation规则 $config[‘add_hotel’] = array( array( ‘field’ => ‘hotel[][hotel_name]’, ‘label’ => ‘Hotel Name’, ‘rules’ => ‘required’ ), array( ‘field’ => ‘hotel[][contact_first_name]’, ‘label’ => ‘First Name’, ‘rules’ => ‘trim|required’ ), array( ‘field’ => ‘hotel[][contact_last_name]’, ‘label’ => […]

在javascript / jQuery中对关联数组执行“Diff”操作?

如果我有两个关联数组,那么对它们的值进行差异的最有效方法是什么? 例如,给定: array1 = { foreground: ‘red’, shape: ‘circle’, background: ‘yellow’ }; array2 = { foreground: ‘red’, shape: ‘square’, angle: ’90’, background: ‘yellow’ }; 我如何检查另一个,以便缺少 或 附加的项目是结果数组。 在这种情况下,如果我想比较array2中的array1,它将返回: array3 = {shape: ‘circle’} 虽然如果我在array1中比较array2,它会返回: array3 = {shape: ‘square’, angle: ’90’} 在此先感谢您的帮助!

JQuery – 将表单数据序列化到Associated Array

如何将HTML表单数据序列化为关联数组,而不是使用jQuery使用$ .serializeArray()生成的数字索引格式? jQuery.serializeArray给出的输出使得使用数字索引键直接选择值很困难。当在表单输入中使用复选框时,可能会发生轻微的索引转换。 serializeArray的输出 [ 0: [name: ‘field-1’, value: ‘val1’], 1: [name: ‘check’, value: ‘val2’], 2: [name: ‘check’, value: ‘val3’] ] 期望的输出 – 更可靠的格式和更简单的价值访问 [ ‘field-1’ : ‘val1’, ‘check’ : [ 0 : ‘val2’, 1 : ‘val3’ ] ]

jquery .serializeArray(); 在顶部添加另一个值以传递给ajax

我正在做以下 var data = $(form).serializeArray(); // now i want to add another value on this data data.username = ‘this is username’; 我想知道如何在执行serializeArray()之后添加另一个值,我尝试了所有我知道的事情,但没有任何东西可以使它工作。 任何想法请。

基于属性将对象数组分成单独的数组

假设我有一个这样的数组: var arr = [ {type:”orange”, title:”First”}, {type:”orange”, title:”Second”}, {type:”banana”, title:”Third”}, {type:”banana”, title:”Fourth”} ]; 我希望将它拆分为具有相同类型的对象的数组: [{type:”orange”, title:”First”}, {type:”orange”, title:”Second”}] [{type:”banana”, title:”Third”}, {type:”banana”, title:”Fourth”}] 但我想一般这样做,所以没有if语句指定橙色或香蕉 // not like this for (prop in arr){ if (arr[prop] === “banana”){ //add to new array } } 思考? JQuery和Underscore都是可以使用的选项。

将textareas字符串值转换为由新行分隔的JavaScript数组

我有一个textarea ,用户最多可以写1000个字符。 我需要获取jQuery(‘#textarea’).val()并创建一个数组,其中每个项目都是textarea值的一行。 这意味着: 这是textarea内部的一个很好的路线。 这是另一条线。 (让我们假设这一行是空的 – 应该被忽略)。 有人在上面留下了2条以上的新线。 应该转换为JavaScript数组: var texts = []; text[0] = ‘This is a nice line inside the textarea.’; text[1] = ‘This is another line.’; text[2] = ‘Someone left more than 2 new lines above.’; 这样就可以很容易地将它们破坏为querystring(这是提供者所需的qs格式): example.com/process.php?q=[“This is a nice line inside the textarea.”,”This is another line.”,”Someone left more […]