My question is it possible to change and array into an object?
The following code counts the number of occurrences of each word in an array.
// function for getting the frequency of each word within a string
function getFreqword(){
var string = tweettxt.toString(), // turn the array into a string
changedString = string.replace(/,/g, " "), // remove the array elements
split = changedString.split(" "), // split the string
words = [];
for (var i=0; i<split.length; i++){
if(words[split[i]]===undefined){
words[split[i]]=1;
} else {
words[split[i]]++;
}
}
return words;
}
Is it possible to change it so that instead of returning an array like this:
[ Not: 1,
long: 1,
left: 2,
grab: 1,
an: 4,
Easter: 5,
bargain: 1,]
instead it returns an object like this? { word: 'Not' num: 1 } etc.
var obj = {};then dot notation add a propertyobj.prop1 = 2;or bracket notation likeobj["prop2"] = 2;the handy thing is that bracket notation can use a string thusvar propName = "prop3"thenobj[propName] = 2;givingobj.prop3 === 2as true.tweetxtvalue ?