简体   繁体   English

jQuery $ .extend无法正常工作:如何正确编码?

[英]JQuery $.extend doesn't work like I would expect: How to code this correctly?

Here is the JQuery extend page. 是JQuery扩展页面。

What I would expect this to do is take two arrays, join them together and create uniform properties for each resulting array element. 我希望这样做是将两个数组连接在一起,并为每个结果数组元素创建统一的属性。 How do I get this to work as expected? 如何使它按预期工作?

var a = [];
a.push({ "id": 1, "text": "one" });
a.push({ "id": 2, "text": "two" });
a.push({ "id": 3, "text": "three" });
var b = [];
b.push({"id":3 , "selected":true});
var c = [];
$.extend(c,a,b);

What I would expect is that the resulting array would include: 我期望的是,结果数组将包括:

{ "id": 1, "text": "one", "selected": false }
{ "id": 2, "text": "two", "selected": false }
{ "id": 3, "text": "three", "selected": true }

but instead it seems to just copy the first array over top of the second: 但是相反,它似乎只是将第一个数组复制到第二个数组之上:

{ "id": 3, "text": null, "selected": true }
{ "id": 2, "text": "two" }
{ "id": 3, "text": "three" }

The documentation includes: 该文档包括:

When we supply two or more objects to $.extend(), properties from all of the objects are added to the target object. 当我们向$ .extend()提供两个或更多对象时,所有对象的属性都将添加到目标对象。

What am I doing wrong, or how would I accomplish this otherwise? 我在做什么错,否则我将如何做到这一点?

EDIT: Jball's suggestion implemented: 编辑:贾巴尔的建议实施:

var a = [];
a.push({ "id": 1, "text": "one" });
a.push({ "id": 2, "text": "two" });
a.push({ "id": 3, "text": "three" });
var b = [];
b.push({ "id": 3, "selected": true });
var c = [];
for (var index = 0; index < a.length; index++) {
   var tempresult = {};
   var tempb = b.filter(
      function (ele, idx, collection) {
         return (collection[idx].id == index + 1);
      });
   if (tempb.length == 0)
      tempb = [{ "id": index + 1, "selected": false }];
   $.extend(tempresult, a[index], tempb[0]);
   c.push(tempresult);
} 

produces: 产生:

[{"id":1, "selected":false, "text": "one"},
 {"id":2, "selected":false, "text": "two"},
 {"id":3, "selected":true,  "text": "three"}]

That's the answer. 那就是答案。 Now I wonder if it can be cleaned up a bit. 现在,我想知道是否可以对其进行清理。

I'm not sure if you noticed it, but the $.extend() function is intended for properties of objects, not elements of an array. 我不确定您是否注意到它,但是$.extend()函数用于对象的属性,而不是数组的元素。

It seems like you need to create a function to loop through the arrays, and call $.extend() on matching elements to get your desired result. 似乎您需要创建一个遍历数组的函数,并在匹配的元素上调用$.extend()以获得所需的结果。 You would have to decide whether you want to add or ignore non-matching elements from the second array. 您必须决定是要添加还是忽略第二个数组中的不匹配元素。

The problem is that jQuery has no idea what elements in your array are matching, and so matches them by index, though I'm not sure why the result for the first item has "text": "three" instead of "text": "one" , unless it is attempting to match by the individual items properties after it does an $.extend() based on index. 问题是jQuery不知道数组中的哪些元素是匹配的,因此按索引匹配它们,尽管我不确定为什么第一项的结果是"text": "three"而不是"text": "one" ,除非它在基于索引执行$.extend()之后尝试按各个项目属性进行匹配。

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

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