Tag: 对象

基于子值合并数组中的对象

我有一个填充了对象的数组。 当具有匹配的特定子值时,如何合并此数组中的对象? 我的数组看起来像这样: var data = [ { prod_name:”test1″, type:”1″, color:”white”, product_id:”5″ }, { prod_name:”test2″, type:”1″, color:”green”, product_id:”7″ }, { prod_name:”test2″, type:”2″, color:”green”, product_id:”7″ }, { prod_name:”test3″, type:”4″, color:”red”, product_id:”8″ }, { prod_name:”test4″, type:”2″, color:”white”, product_id:”21″ } ]; 我想基于匹配的product_id合并对象。 如果值不相同,我想保留BOTH值,用逗号分隔。 所以前一个数组的结果将成为: [ { prod_name:”test1″, type:”1″, color:”white”, product_id:”5″ }, { prod_name:”test2″, type:”1,2″, color:”green”, product_id:”7″ }, { […]

用jQuery实现Javascript高效的搜索数组

我的JavaScript知识存在差距。 我想搜索特定值的对象值数组并返回它。 对于我一直在编写JavaScript的那一年,我一直在实现它: var itemClicked = (function(){ var retval; //Note self.inventory.itemsArray is an array of JS objects $(self.inventory.itemsArray).each(function(i){ if(parseInt(this.id) === parseInt(idOfItem)){ retval = this; return false; } }); return retval; })(); 它有效,但我确信任何事情都有更优雅的方式。 请告诉我! 编辑 – 解决方案 感谢@gdoron,他的回答如下。 var myVar = $(self.owner.itemsArray).filter(function(){ return parseInt(this.id) == parseInt(recItemID); }).get(0); 注意:最后添加了.get(0) ,因为myVar被包装为jQuery对象。

比较jQuery对象

我正在使用选择器来获取一组对象(0或更多): var $openMenus = $Triggers.filter(“.trigger-hover”); 然后我有一个事件附加到上面的对象中可能或可能不在的项目。 在那个我希望将触发事件的项目与c进行比较的事件中 $([selector]) .focus(function(){ var $thisMenu = $(this); $openMenus.each(function(){ if ($(this) != $thisMenu ){ [do something] } }) }) 这不行。 虽然多个jQuery对象可能会引用相同的DOM对象,但它们实际上是单独的jQuery对象,并且永远不会比较true。 鉴于此,处理这个问题的方法是什么? 如何有两个jQuery对象并比较它们以查看一个jQuery对象是否引用与另一个相同的DOM元素? 我可以给每个项目,我正在尝试选择一个ID,但我想知道是否有其他方法去做,而不必添加更多的HTML。

从jQuery .each()中的javascript对象中删除它

我从以下javascript对象中删除this (特定的“事件”)时遇到问题, this是来自jquery .each()循环。 weatherData: { “events”:{ “Birthday”:{ “type”:”Annual”, “date”:”20120523″, “weatherType”:”clouds”, “high”:”40″, “low”:”30″, “speed”:”15″, “direction”:”0″, “humidity”:”0″ }, “Move Out Day”:{ “type”:”One Time”, “date”:”20120601″, “weatherType”:”storm”, “high”:”80″, “low”:”76″, “speed”:”15″, “direction”:”56″, “humidity”:”100″ } }, “dates”:{ “default”:{ “type”:”clouds”, “high”:”40″, “low”:”30″, “speed”:”15″, “direction”:”0″, “humidity”:”0″ }, “20120521”:{ “type”:”clear”, “high”:”60″, “low”:”55″, “speed”:”10″, “direction”:”56″, “humidity”:”25″ } } } 这是.each()循环的缩小版本: $.each(weatherData.events, function(i){ if(this.type == […]

匹配数组中的对象并进行合并

更新: 我有一个名为cars的对象数组,其中包含带有关于汽车属性数据的li标签(例如价格,汽车类型等)。 我的目标是根据某些标准将这些汽车整合到一个单一的列表中。 要求 快速表现 保持相同的汽车arrays结构 主要目标:匹配预付费和零售清单 – 将零售清单中的HTML(例如按钮和定价信息)合并到预付清单中。 看到: 如果匹配(基于IF语句中的标准),则删除匹配的列表,而不使用“listing-prepaid”类,并使用零售列表中的某些信息更新匹配的预付费列表。 汽车arrays: 52CompactNissan Versa or similar†Car on AirportUnlimited MilesPick-upSFB: Orlando Sanford Intl AirportDrop-offSame as pick-upPay Now & Save 2%$25$24/dayPay NowTotal: $34 41EconomyChevrolet Spark or similar†Unlimited MilesPick-up3575 Vineland Road, Orlando, FLDrop-offSame as pick-upPay Now & Save 9%$33$29/dayPay NowTotal: $36 52CompactNissan Versa or similar†Car on AirportFree CancellationPay […]

在javascript中过滤对象数组

我想按属性值过滤JavaScript对象。 这是一个场景: 下面的数组是我正在使用的一个小例子 var array = [{ “Title”: “July 13 – July 19 2014”, “displayAd_imp”: “15,242,505”, “Date”: “2014-07-17T00:00:00.000Z”, “WeekNo”: 29 }, { “Title”: “July 20 – July 26 2014”, “displayAd_imp”: “15,942,705”, “Date”: “2014-07-24T00:00:00.000Z”, “WeekNo”: 30 }, { “Title”: “July 27 – Aug 2 2014”, “displayAd_imp”: “15,683,545”, “Date”: “2014-07-31T00:00:00.000Z”, “WeekNo”: 31 }, { “Title”: “Aug 3 […]

jQuery / javascript事件 – 原型事件处理程序

以下代码不起作用,因为我直觉地期望它: function MyObject(input) { input.change(this._foo); this.X = undefined; } MyObject.prototype._foo = function() { alert(“This code is never called”); // but if it did this.X = true; } var test_input = $(“input#xyz”); // a random, existing input var m = MyObject(test_input); // attach handler (or try to) test_input.change(); // trigger event alert(mX); // undefined 我希望调用_foo() (如果发生this情况, […]

jQuery – 如何在另一个对象中存储和访问对象

我试图将对象存储为另一个对象中的属性,但似乎无法访问它。 可能吗? $(document).ready(function() { //create a TestObject function TestObject() { this.testProperty = “green”; } //and an instance of it var testObject = new TestObject(); //attach this instance to the div as a property //if it could be attached another way perhaps that’s the answer?? $(‘#test’).attr(“obj”, testObject); alert(testObject.testProperty);//works – obviously alert($(‘#test’).attr(“obj”).testProperty); //does not work var o […]

使用另一个对象数组过滤对象数组

这个问题类似于这个带有循环的对象的Jqueryfilter数组,但这次我需要用一个对象数组来做filter。 例: 我有一个像这样的对象数组: myArray = [ { userid: “100”, projectid: “10”, rowid: “0” }, { userid: “101”, projectid: “11”, rowid: “1”}, { userid: “102”, projectid: “12”, rowid: “2”}, { userid: “103”, projectid: “13”, rowid: “3” }, { userid: “101”, projectid: “10”, rowid: “4” } …] 我想用这样的数组过滤它: myFilter = [ { userid: “101”, projectid: “11” }, […]

“过滤”JSON获取唯一键并获取所有相关值

什么是找到组中所有可能相关值的最佳方法。 var table = [ {group:”a”, stuff:”new”}, {group:”a”, stuff:”old”}, {group:”b”, stuff:”newOld”}, {group:”b”, stuff:”old”}, {group:”c”, stuff:”new”}, {group:”c”, stuff:”old”}, {group:”c”, stuff:”newOld”}, ]; 我想填充一个包含唯一group值的下拉列表。 在选择时我想使用所有相关的stuff进行进一步处理。 并且还想添加一个包含所有stuff的组。 例如 on selection of all -> new, old, newOld a -> new, old b -> newOld, old c -> new, old, newOld