Reputation: 309
I'm working on a project to parse RSS feeds from several sites into an aggregated job board, using rss-parser
The feeds use different formats for title
, and as such the parsed JSON has differing formats too. See sample of three different formats below.
title: Roundtrip: Senior Product Designer
title: Frontend Developer
title: Unreal is looking for a Product Designer
What I'm looking to do is to unify the title / company format
Roundtrip: Senior Product Designer
into company: Roundtrip
and title: Senior Product Designer
Unreal is looking for a Product Designer
into company: Unreal
and title: Product Designer
.Currently they're merged to an array in the state using
this.setState(prevState => ({
data: this.state.data.concat(feed.items)
}));
But this puts me in the position where the differing title formats become very apparent.
How would I go about splitting those strings and then merging them to a combined array?
Upvotes: 1
Views: 62
Reputation: 1475
You could run a map over the items to get a new array with title and company keys.
let items = [{title:'Roundtrip: Senior Product Designer'},{title:'Frontend Developer'},{title:'Unreal is looking for a Product Designer'}]
console.log(items.map(item => {
const colonSeperated = item.title.split(':')
const phraseSeperated = item.title.split('is looking for ')
if(colonSeperated.length == 2){
return ({
company:colonSeperated[0],
title: colonSeperated[1].trim(' ')
})
}
else if(phraseSeperated.length ==2){
const phraseSeperatedTitle =
phraseSeperated[1].startsWith('an')?
phraseSeperated[1].substr(2):phraseSeperated[1].substr(1)
return({
company:phraseSeperated[0].trim(' '),
title: phraseSeperatedTitle.trim(' ')
})
}
else {
return ({
company:null,
title:item.title
})
}
}
))
Upvotes: 1