【问题标题】:How do I delete an array inside an array?如何删除数组中的数组?
【发布时间】:2016-07-02 14:55:26
【问题描述】:

我想删除一个数组条目。 已经用过拼接方法了,还是不行。

var clients = new Array();
//-----

var tmp = new Array();
tmp["connection"] = connection;
tmp["authentificated"] = 1;
tmp["username"] = rows[0].username;
tmp["rank"] = rows[0].rank;
clients.push(tmp);

这不起作用:

clients.splice(index, 1);

你有什么想法,错误在哪里?

提前致谢。

【问题讨论】:

  • 好吧,index 上有什么内容?
  • 最好将tmp用作对象而不是数组。
  • 数组中有一个数组。致电delete clients[0]['username']; 或使用对象。
  • “这行不通”。显示minimal reproducible example
  • 谢谢大家。你是对的。对“索引”的解释会很有帮助。

标签: javascript arrays


【解决方案1】:

你可以像下面这样使用。

var clients = new Array();


var tmp = new Array();
tmp["connection"] = "hi";
tmp["authentificated"] = 1;

clients.push(tmp);

var tmp1 = new Array();
tmp1["connection"] = "hi";
tmp1["authentificated"] = 2;

clients.push(tmp1);
var index = 0;
delete clients[index];

console.log(clients);

但 delete 的问题是数组的长度将保持不变,因为 delete 只会从数组中的元素中删除对象

【讨论】:

    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    相关资源
    最近更新 更多