Reputation: 9
I need to filter an object by only taking in results that have ".com" in the string whilst also removing a string from it. I can remove the string and the results go into my DB perfectly however I am struggling with the filter. Is there a way I can call both the replace and filter functions inside the same variable being companyFilter?
Assume the object is:
[{
company: 'amazon'
companyurl:'amazon.com'
}
{
company: '400 prize money'
companyurl:'400 prize money'
}
{
company: 'facebook'
companyurl:'facebook.com'
}
{
company: 'google'
companyurl:'google.com'
}
]
const newObject = data.map((item) => {
const companyNoCom = item['companyurl'].replace(/hackerone.com\//g, "")
//const companyFilter = data.filter((item) => item['company'].includes('.com'))
newItem = {...item,
company: companyNoCom,
}
return newItem
})
console.log(newObject)
The required output would be:
[{
company: 'amazon'
companyurl:'amazon.com'
}
{
company: 'facebook'
companyurl:'facebook.com'
}
company: 'google'
companyurl:'google.com'
}
]
Upvotes: 0
Views: 63
Reputation: 3231
you can use String.prototype.endsWith()
to check if the end of the string is .com
and filter by this:
const arr = [{
company: 'amazon',
companyurl:'amazon.com'
},
{
company: '400 prize money',
companyurl:'400 prize money'
},
{
company: 'facebook',
companyurl:'facebook.com'
},
{
company: 'google',
companyurl:'google.com'
}
];
const res = arr.filter(({ companyurl }) => companyurl.endsWith('.com'));
console.log(res);
Upvotes: 0
Reputation: 2857
Array.map()
ALWAYS returns ALL results but allows changes to each object within the array.
Array.filter()
on the other hand ONLY filters and cannot change individual objects within the array and should only return a boolean.
If you need to first make changes to the data in order to determine if it should be included, run .map
first, then .filter
on the results of map()
. The original data will be unchanged and newObject
will contain your results:
var newObject = data
.map((item) => {
item['companyurl'] = item['companyurl'].replace(/hackerone.com\//g, "");
return item;
})
.filter((item) => item['companyurl'].includes('.com'));
// Note. the original data array will not be changed.
Upvotes: 1
Reputation: 2760
const data = [{
company: 'amazon',
companyurl:'amazon.com'
},
{
company: '400 prize money',
companyurl:'400 prize money'
},
{
company: 'facebook',
companyurl:'facebook.com'
},
{
company: 'google',
companyurl:'google.com'
}
]
const result = data.filter(item => item.companyurl.includes(".com")).map(item => ({...item, company: item.companyurl.split(".com")[0]}))
console.log(result)
EDIT: I made an adjustment to match desired output, so with this one you would always get company value coming from companyurl after striping .com part
Upvotes: 0
Reputation: 20006
const data = [
{ company: "amazon", companyurl: "amazon.com" },
{ company: "400 prize money", companyurl: "400 prize money" },
{ company: "facebook", companyurl: "facebook.com" },
{ company: "google", companyurl: "google.com" },
];
const newObject = data.filter((item) => item.companyurl.includes(".com"));
console.log(newObject);
Upvotes: 0