如何从数组删除对象并保存供以后使用? [英] How to remove object from Array and save it for use later?

查看:146
本文介绍了如何从数组删除对象并保存供以后使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本来我的问题是,<一个href=\"http://stackoverflow.com/questions/10024866/remove-object-from-array-using-javascript\">How从jQuery的阵列 的对象。 &LT;找到答案,从@nnnnnn之一。使用 $。gr​​ep的

我现在新的问题是如何将你保存刚才删除的对象?

My new question now is how would you save the object that you just removed?

原因是我希望能够推救对象回阵列如果用户决定保留它。

Reason is I wish to be able to push that saved Object back into the Array if the user decides to keep it.

我的网​​络数组:

控制台:结果
网​​络阵列= [对象的对象],[对象的对象]

networks: Array[2]
    0: Object
      count: 1
      id: "6"
      label: "CompanyName"
      type: "Organization"

    1: Object
      count: 1
      id: "12622"
      label: "MyGroup"
      type: "Group"

这是我使用这个在网络数组中寻找对方的回答中,找到对象与类型的组织并删除它。

From the other answer I'm using this to look in the networks array, find the Object with the type of "Organization" and remove it.

网​​络= $ .grep(网络功能(O,I){返回o.type ===组织;},真实);

有没有一种简单的方法来保存整个对象?因此,它可以推后?

Is there a simple way to save that entire Object? So it can be pushed back in?

感谢您抽空看看!

推荐答案

您可以在回调内部的对象分配给一个变​​量。这不是一个很干净的解决方案,因为这样的回调应该不会有副作用IMO,但它工作未做更大的变化到code:

You could assign the object inside the callback to a variable. This isn't a very clean solution, since such callbacks shouldn't have side effects IMO, but it works without making bigger changes to your code:

var obj;
networks = $.grep(networks, function(o,i) {
  if (o.type === "Organization") {
      obj = o;
      return true;
  }
  return false;
}, true);

还是有点短(多为迷惑不知道逗号操作符谁也人):

or a bit shorter (and more confusing for people who don't know about the comma operator):

var obj;
networks = $.grep(networks, function(o,i) { 
    return o.type === "Organization" ? ((obj = o), true) : false;
}, true);

这篇关于如何从数组删除对象并保存供以后使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆