【问题标题】:how to find particular element from array and remove all next elements [duplicate]如何从数组中找到特定元素并删除所有下一个元素[重复]
【发布时间】:2018-05-22 12:22:22
【问题描述】:

我想从一个数组中找到一个特定元素并从该特定数组中删除所有下一个元素。

下面是我的代码:

var data1 = [a,b,c,d,e,f,g,h];

var data2 = c; // this is element, that i want to find from data1 and 
remove all next element i.e. d, e, f, g, h .

我想要输出如下:var res = [a,b,c];

谢谢,

【问题讨论】:

标签: javascript jquery


【解决方案1】:

使用splice()。您需要在data1 中获取data2 值的索引,然后使用该索引删除data1 之后的所有元素。

var data1 = ['a','b','c','d','e','f','g','h'];
var data2 = 'c';
data1.splice(data1.indexOf(data2)+1, data1.length);
console.log(data1);

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 2019-08-02
    • 2016-02-12
    • 2012-06-19
    相关资源
    最近更新 更多