Stack Overflow Asked by Riko Pramayudi on November 7, 2021
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…
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);
Answered by Ariful on November 7, 2021
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]))
Answered by Mr. Polywhirl on November 7, 2021
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))
Answered by Giovanni Esposito on November 7, 2021
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());
}
});
Answered by Maximillion Bartango on November 7, 2021
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] ) ) ) );
Answered by KooiInc on November 7, 2021
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)
Answered by elvira.genkel on November 7, 2021
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);
Answered by Nikita Madeev on November 7, 2021
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]
Answered by Nicholas Harder on November 7, 2021
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)
Answered by soltex on November 7, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP