var array2 = [3,6,7,8,1];
array2.slice(1);
will produce [6, 7, 8, 1]
Note that slice copies the array's contents only one level deep. What that means technically is that nested arrays or objects within the output of slice are still references to the same arrays or objects contained in the original parent array. What that means to you practically is that changing an element or property in the nested arrays/objects returned from slice will also change the same element or property within the nested array or object contained in the original parent array.
javascript - How do you search an array for a substring match? - Stack Overflow
Is there a javascript method to find substring in an array of strings? - Stack Overflow
How do I only replace the first instance of a substring?
ELI5: Using .map and .substring
Videos
People here are making this waaay too difficult. Just do the following...
myArray.findIndex(element => element.includes("substring"))
findIndex() is an ES6 higher order method that iterates through the elements of an array and returns the index of the first element that matches some criteria (provided as a function). In this case I used ES6 syntax to declare the higher order function. element is the parameter of the function (which could be any name) and the fat arrow declares what follows as an anonymous function (which does not need to be wrapped in curly braces unless it takes up more than one line).
Within findIndex() I used the very simple includes() method to check if the current element includes the substring that you want.
If you're able to use Underscore.js in your project, the _.filter() array function makes this a snap:
// find all strings in array containing 'thi'
var matches = _.filter(
[ 'item 1', 'thing', 'id-3-text', 'class' ],
function( s ) { return s.indexOf( 'thi' ) !== -1; }
);
The iterator function can do whatever you want as long as it returns true for matches. Works great.
Update 2017-12-03:
This is a pretty outdated answer now. Maybe not the most performant option in a large batch, but it can be written a lot more tersely and use native ES6 Array/String methods like .filter() and .includes() now:
// find all strings in array containing 'thi'
const items = ['item 1', 'thing', 'id-3-text', 'class'];
const matches = items.filter(s => s.includes('thi'));
Note: There's no <= IE11 support for String.prototype.includes() (Edge works, mind you), but you're fine with a polyfill, or just fall back to indexOf().
var array= ["abcdefg", "hijklmnop"];
var newArray = array.filter(function(val) {
return val.indexOf("mnop") !== -1
})
console.log(newArray)
a possible solution is filtering the array through string.match
var array= ["abcdefg", "hijklmnop"];
var res = array.filter(x => x.match(/mnop/));
console.log(res)