JQuery $.extend 不像我期望的那样工作:How to code this right?

发布于 2024-10-01 02:58:47 字数 1635 浏览 4 评论 0原文

这里是JQuery扩展页面。

我希望这样做的是采用两个数组,将它们连接在一起并为每个结果数组元素创建统一的属性。我怎样才能让它按预期工作?

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);

我期望的是结果数组将包括:

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

但它似乎只是将第一个数组复制到第二个数组之上:

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

文档包括:

当我们向 $.extend() 提供两个或多个对象时,属性将所有对象添加到目标对象中。

我做错了什么,否则我将如何完成此操作?

编辑:Jball 的建议已实施:

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);
} 

产生:

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

这就是答案。现在我想知道是否可以清理一下。

Here is the JQuery extend page.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪眸﹌ 2024-10-08 02:58:47

我不确定您是否注意到了,$.extend() 函数适用于对象的属性,而不是数组的元素。

似乎您需要创建一个函数来循环数组,并在匹配元素上调用 $.extend() 以获得所需的结果。您必须决定是否要添加或忽略第二个数组中的不匹配元素。

问题是 jQuery 不知道数组中的哪些元素匹配,因此按索引匹配它们,尽管我不确定为什么第一个项目的结果有 "text": "two" 而不是 "text": "one",除非它在基于索引执行 $.extend() 后尝试通过各个项目属性进行匹配。

I'm not sure if you noticed it, but the $.extend() function is intended for properties of objects, not elements of an array.

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文