Reputation: 131
i have question about getting character of string after space where the string is value of array like this..
arr = ['aaa: 2' , 'aaa: 5', 'aaa: 6', 3 , 7 , 8];
output = arr.filter(function (p) {
if (!Number(p)) { // get string value
return p.includes('aaa').split(' ').pop();
}
});
console.log(output)
i got error "TypeError: p.includes(...).split is not a function"
if i remove the .split(' ').pop();
array['aaa: 2','aaa: 5','aaa: 6']
i just want the output like this
array [2,5,6]
can anyone who's have experience on same problem help me? i'm stuck. thank you guys...
Upvotes: 2
Views: 589
Reputation: 121
if you want to get a customize array from your main array then you can use reduce method.
const arr = ['aaa: 2', 'aaa: 5', 'aaa: 6', 3, 7, 8];
const output = arr.reduce((value, key) => {
if (!Number(key) && key.includes('aaa')) {
value.push(parseInt(key.split(' ').pop()));
}
return value;
}, []);
console.log(output);
Upvotes: 1
Reputation: 1599
Firstly, .filter is the wrong method to use here, it either expects a true or false to keep/remove the given item from the array. Use .map instead.
Secondly, .includes returns true or false so trying to split a boolean will not work
if you want to remove the numbers as mentioned in your last edit, filter first. Try this:
// first filter out numbers
const output = arr.filter(function(p) {
return !Number(p)
}
// then get the numbers out
output = output.map(function(p) {
if (p.includes('aaa')) {
return Number(p.split(' ').pop());
}
});
Upvotes: 1
Reputation: 4380
String.prototype.includes() return bool value, which has no method split
Array.prototype.filter() takes:
Function is a predicate, to test each element of the array. Return true to keep the element, false otherwise.
const arr = ['aaa: 2', 'aaa: 5', 'aaa: 6', 3, 7, 8];
const output = arr
.filter((p) => {
return Number(p) ? false : p.includes('aaa');
})
.map((p) => Number(p.split(' ').pop()));
console.log(output);
Upvotes: 2
Reputation: 48693
If the value is a string, you can use the following regular expression to extract the digit following the space e.g.
/^\w+:\s(\d+)$/
Match group #1 will be the digit, you will just need to parse it to an integer.
const transform = (arr) =>
arr.map(val => typeof val === 'string'
? parseInt(val.match(/^\w+:\s(\d+)$/)[1], 10)
: val)
console.log(transform(['aaa: 2' , 'aaa: 5', 'aaa: 6', 3 , 7 , 8]))
Upvotes: 0
Reputation: 11156
Ciao, try this:
var arr = ['aaa: 2' , 'aaa: 5', 'aaa: 6', 3 , 7 , 8];
console.log(arr.map(el => {
if(typeof el === "string") {
return parseInt(el.split(": ")[1]);
}
}).filter(el => el!== undefined))
Upvotes: 0
Reputation: 122946
Looks like you need the numbers from the array? Use Array.map
and a bit of RegEx
magic:
console.log(
JSON.stringify( ['aaa: 2', 'aaa: 5', 'aaa: 6', 3 , 7 , 8]
.map( v => /:\s+\d$/.test(v) ? Number(v.split(": ")[1]) : v) ) );
// only numbers from strings?
console.log(
JSON.stringify( ['aaa: 2', 'aaa: 5', 'aaa: 6', 3 , 7 , 8]
.filter( v => isNaN(+v) )
.map( v => Number( v.split(": ")[1] ) ) ) );
Upvotes: 0
Reputation: 1333
Array.prototype.filter() function is intended to only filter an array, without modifications. To combine modifications and filetring use reduce instead:
const arr = ['aaa: 2' , 'aaa: 5', 'aaa: 6', 3 , 7 , 8];
const output = arr.reduce((result, current) => {
if (!Number(current)) { // get string value
if(current.includes('aaa')) {
return [...result, current.split(' ').pop()];
}
}
return result
}, []);
console.log(output)
Upvotes: 2
Reputation: 1558
Creating a regex that groups the number, and then extracting it would work.
const regex = /aaa:\s(\d+)/
const matches = regex.exec(p)
return matches && matches.length > 0 && matches[0]
Upvotes: 0
Reputation: 3551
You can use .reduce:
const arr = ['aaa: 2' , 'aaa: 5', 'aaa: 6', 3 , 7 , 8];
const output = arr.reduce((acc, arrayValue) => {
const [key, value] = arrayValue.toString().split(': ');
if (key.includes('aaa')) {
acc.push(parseInt(value))
}
return acc;
}, []);
console.log(output)
Upvotes: 0