[["Django UnChainers","AA-24010"],["General","AA-26191"]]
I have one array in above form. I want to retrieve all the value with prefix AA (which are in the second position). Is there any way where I can fetch the value by passing prefix?.
I know the way where I can get the value by passing index but may be tomorrow index can get change so is it possible to fetch the value by passing prefix?
In case OP wants a function to do this.
function(arr, pattern){ return arr.map(function(x){ return x.filter( word => ~ word.indexOf(pattern)) }); }
var arr = [ [ "Django UnChainers", "AA-24010" ], [ "General", "AA-26191" ]]; var list = arr.map(function(x){ if(~(x[1].indexOf('AA'))){ return x[1]; } }); console.log(list);
In case the index changes in future, iterate through each string and check for the "AA" string. Check the below code.
var arr = [ [ "Django UnChainers", "AA-24010" ], [ "General", "AA-26191" ]]; var list = arr.map(function(x){ return x.filter( word => ~ word.indexOf('AA')) }); console.log(list);
this is shorter
var = [nested array]
a.filter(x => x[1].startsWith('AA'))
//in case you are not sure about the index
a.filter(x => x.filter(y => y.startsWith('AA').length > 0))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.