javascript - 获取另一个数组中包含的所有单词(拆分句子)的数组
问题描述
我有这个数组:
this.places = [{"full_name":"joe","title1":"teacher","description":"the best teacher"},
{"full_name":"mary","title1":"student","description":"great student"}];
我需要得到这个:
this.items = [{"word":"joe"},{"word":"teacher"},{"word":"the"},{"word":"best"},{"word":"teacher"},
{"word":"mary"},{"word":"student"},{"word":"great"},{"word":"student"} ];
我现在有这个:
this.places.forEach(item => {
this.items = results.concat(item.full_name.toLocaleLowerCase().split(" "))
.concat(item.title1.toLocaleLowerCase().split(" "))
.concat(item.description.toLocaleLowerCase().split(" "));
});
结果:
["joe","teacher","the","best","teacher","mary","student","great","student"];
你能帮我得到完整的最终所需的数组吗?
解决方案
减少的另一种方法:
this.places = [
{"full_name":"joe","title1":"teacher","description":"the best teacher"},
{"full_name":"mary","title1":"student","description":"great student"}
];
this.items = this.places.reduce((acc, place) => {
Object.values(place).toString().split(/\s+|,/).forEach(word => acc.push({ word }));
// This might be slightly faster
// Object.values(place).toString().replace(/\w+/g, word => acc.push({ word }));
return acc;
}, []);
console.log(this.items);
推荐阅读
- javascript - 自动替换 html 代码的 Chrome 扩展
- reactjs - .env 变量在 create-react-app UI 中显示为未定义
- javascript - 如何从 CSS 样式中设置 JS 变量
- django - Django TemplateSyntaxError 无法解析余数:使用 {% if MyModel.objects.filter(id=maxid).values('col2') = something %} 时出错
- php - SQL Select:加入 3 个表并对最后一个表进行计数
- python-3.x - 功能 service_account.Credentials.from_service_account_info() 不起作用
- c# - AutoMapper - 嵌套集合未映射
- c++ - 模板类。如何实现这个?c++
- python - Python 中的 MemoryError 通过使用 mmap 和 re.findall 搜索大文件
- r - 将参考点和线添加到 geom_line() 图