Javascript 获取包含在另一个数组中的所有单词(拆分句子)的数组
我有这个阵列:Javascript 获取包含在另一个数组中的所有单词(拆分句子)的数组,javascript,arrays,json,object,Javascript,Arrays,Json,Object,我有这个阵列: 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":"t
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.forEach(item => {
this.items.push(`{"word": results.concat(item.full_name.toLocaleLowerCase().split(" "))
.concat(item.title1.toLocaleLowerCase().split(" "))
.concat(item.description.toLocaleLowerCase().split(" "))};
});
这应该可以做到
this.places.forEach(item => {
this.items.push(`{"word": results.concat(item.full_name.toLocaleLowerCase().split(" "))
.concat(item.title1.toLocaleLowerCase().split(" "))
.concat(item.description.toLocaleLowerCase().split(" "))};
});
this.places=[{“全名”:“乔”,“标题1”:“老师”,“描述”:“最好的老师”},
{“全名”:“玛丽”,“标题1”:“学生”,“描述”:“伟大的学生”}];
此参数为.items=[];
用于(此位置的常量对象)
用于(对象值的常数v(obj))
v、 toLocaleLowerCase().split(/\s+/).forEach(w=>this.items.push({word:w}));
log(JSON.stringify(this.items))代码>
this.places=[{“全名”:“乔”,“标题1”:“老师”,“描述”:“最好的老师”},
{“全名”:“玛丽”,“标题1”:“学生”,“描述”:“伟大的学生”}];
此参数为.items=[];
用于(此位置的常量对象)
用于(对象值的常数v(obj))
v、 toLocaleLowerCase().split(/\s+/).forEach(w=>this.items.push({word:w}));
log(JSON.stringify(this.items))代码>使用reduce的另一种方法:
this.places=[
{“全名”:“乔”,“标题1”:“老师”,“描述”:“最好的老师”},
{“全名”:“玛丽”,“标题1”:“学生”,“描述”:“伟大的学生”}
];
this.items=this.places.reduce((acc,place)=>{
Object.values(place.toString().split(/\s+|,/).forEach(word=>acc.push({word}));
//这可能会稍微快一点
//Object.values(place.toString().replace(/\w+/g,word=>acc.push({word}));
返回acc;
}, []);
console.log(this.items)代码>使用reduce的另一种方法:
this.places=[
{“全名”:“乔”,“标题1”:“老师”,“描述”:“最好的老师”},
{“全名”:“玛丽”,“标题1”:“学生”,“描述”:“伟大的学生”}
];
this.items=this.places.reduce((acc,place)=>{
Object.values(place.toString().split(/\s+|,/).forEach(word=>acc.push({word}));
//这可能会稍微快一点
//Object.values(place.toString().replace(/\w+/g,word=>acc.push({word}));
返回acc;
}, []);
console.log(this.items)下面的代码>解决方案有效
this.places = [{"full_name":"joe","title1":"teacher","description":"the best teacher"}{"full_name":"mary","title1":"student","description":"great student"}];
this.items = [];
this.places.forEach(el=>Object.values(el).toString().toLocaleLowerCase().split(/[ ,]+/).forEach(word =>this.items.push({word:word})));
console.log(this.items)
下面的解决方案有效
this.places = [{"full_name":"joe","title1":"teacher","description":"the best teacher"}{"full_name":"mary","title1":"student","description":"great student"}];
this.items = [];
this.places.forEach(el=>Object.values(el).toString().toLocaleLowerCase().split(/[ ,]+/).forEach(word =>this.items.push({word:word})));
console.log(this.items)
您是否正在尝试为以后做一些与map/reduce相关的事情?例如,计算书籍或图书馆中的单词及其频率。。。因此,现在你可以映射,然后稍后你会将你的10页或你的书的映射减少到{joe:12}
,然后进一步减少其他人的结果。你是否尝试稍后做一些与映射/减少相关的事情?例如,计算书籍或图书馆中的单词及其频率。。。因此,现在您可以映射,然后稍后您可以将您的10页或书籍的映射减少到{joe:12}
,然后进一步减少其他人的结果