这是我的代码。我已经有一个句子数组,即userTimeline
。我想做的是使用以Aquarius开头的句子过滤userTimeline
数组,并将其提取到新数组中。我怎么做?
var userTimeline = [];
var params = {
screen_name: "ZodiacFacts"
};
cb.__call(
"statuses_userTimeline",
params,
function (reply, rate, error){
//console.log(reply);
for(var i = 0; i < reply.length; i++){
userTimeline.push(reply[i].text);
}
console.log(userTimeline);
}
);
最佳答案
查看您的代码,我认为您的句子数组是“ reply”,而您要创建的新数组是userTimeline。现在,如@dhilt所述,您应该使用filter。
var userTimeline = [];
var params = {
screen_name: "ZodiacFacts"
};
cb.__call(
"statuses_userTimeline",
params,
function (reply, rate, error){
userTimeline = reply.filter(function (value) {
return /^Aquarius/i.test(value);
});
}
);
console.log(userTimeline); // This array has all the sentences which starts with 'Aquarius'
关于javascript - 如何过滤以相同单词开头的句子数组并使用JavaScript创建新数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47047535/