Remove all elements from array that do not start with a certain string
Modification to erisco's Functional approach,
array_filter($signatureData[0]["foo-"], function($k) {
return strpos($k, 'foo-abc') === 0;
}, ARRAY_FILTER_USE_KEY);
this worked for me.
remove all items in array that start with a particular string
Simply use Array.filter:
arr = arr.filter(function (item) {
return item.indexOf("ftp_") !== 0;
});
Edit: for IE9- support you may use jQuery.grep:
arr = $.grep(arr, function (item) {
return item.indexOf("ftp_") !== 0;
});
Remove all elements from array of strings that do not contain IN
You can use ES5 filter
method:
arr = arr.filter(function(s){
return ~s.indexOf("IN");
});
And using ES6 arrow functions, it can be simplified to:
arr = arr.filter(s=>~s.indexOf("IN"));
Remove all elements from array that match specific string
Simply use the Array.prototype.filter() function for obtain elements of a condition
var array = [1,2,'deleted',4,5,'deleted',6,7];
var newarr = array.filter(function(a){return a !== 'deleted'})
Update: ES6 Syntax
let array = [1,2,'deleted',4,5,'deleted',6,7]
let newarr = array.filter(a => a !== 'deleted')
How to remove all element from array except the first one in javascript
You can set the length
property of the array.
var input = ['a','b','c','d','e','f'];
input.length = 1;
console.log(input);
Javascript array search and remove string?
I'm actually updating this thread with a more recent 1-line solution:
let arr = ['A', 'B', 'C'];
arr = arr.filter(e => e !== 'B'); // will return ['A', 'C']
The idea is basically to filter the array by selecting all elements different to the element you want to remove.
Note: will remove all occurrences.
EDIT:
If you want to remove only the first occurence:
t = ['A', 'B', 'C', 'B'];
t.splice(t.indexOf('B'), 1); // will return ['B'] and t is now equal to ['A', 'C', 'B']
Remove all matching elements from array
Use Array.prototype.filter
, and check that it doesn't include "lol"
:
const result = ["onelolone","twololtwo","three"].filter(ele => !ele.includes("lol"))
console.log(result)// ["three"]
How to remove all element after a specific element of array?
You can do this easily using indexOf and slice methods of array in javascript.
var arr = ["AAA", "BBB", "CCC", "AAA", "BBB", "CCC", "AAA", "BBB", "CCC", "DDD"];
var firstElem = arr[0];
var secondIndex = arr.indexOf(firstElem, 1);
// Output: ["AAA", "BBB", "CCC"]
var newArr = (secondIndex != -1) ? arr.slice(0, secondIndex) : arr;
Working Fiddle: https://jsfiddle.net/4f25wyaj/2/
Related Topics
Initializing PHP Class Property Declarations With Simple Expressions Yields Syntax Error
"Vertical" Regex Matching in an Ascii "Image"
How to Access MySQL Result Set Data With a Foreach Loop
Resetting Array Pointer in Pdo Results
How to Remove Extension from String (Only Real Extension!)
Get the Date of Next Monday, Tuesday, etc
PHP Get All the Images from Url Which Width and Height ≫=200 More Quicker
Ternary Operator and String Concatenation Quirk
Gcm Notifications Not Receiving When App Is in Background Mode in Ios
Execute Raw SQL Using Doctrine 2
How to Setup Elasticsearch Index Structure With Multiple Entity Bindings
Is There Any Particular Difference Between Intval and Casting to Int - '(Int) X'
How to Disable Output Buffering in PHP
How to Replace Text Urls and Exclude Urls in HTML Tags
How to Get Enum Possible Values in a MySQL Database
Any Way to Return PHP 'Json_Encode' With Encode Utf-8 and Not Unicode