So, here's the thing.
I need to read a text and count the recurrence of each individual word. After that, I need to do some manipulations and remove some values.
I found this code in a similar question:
let text = 'Lorem ipsum dolor sit amet.'
let words = text.split(" ");
let obj = {};
let i = 0;
let len = words.length
for (i = 0; i < len; i ) {
if (obj[words[i]] === undefined) {
obj[words[i]] = 1;
} else {
obj[words[i]] ;
}
}
console.log(obj);
And it works fine, except I can access it as a whole, but not individual values (probably because I'm not well versed in JavaScript).
Then I thought about using a simple approach, making an object with the properties 'word' and 'count' and pushing into an array:
let text = 'Lorem ipsum dolor sit amet.'
let words = text.split(" ");
let array = [];
let obj = [];
let i = 0;
let len = words.length
for ( i=0 ; i<words.length ; i ){
obj.word = words[i];
obj.count = 1;
array.push(obj);
}
console.log(array);
But somehow I get a [circular object Array] result.
So, I wanted to know what is wrong with either of the codes, or a possible alternative solution.
CodePudding user response:
Firstly, you define object
as an array and then try to add a property. That is part of the problem. The other is you're trying to reuse the same object every time you loop - you are changing the word and count, but it's the same object, which is causing the circular error.
Here's one way to approach it. Do your loop and try to find an existing reference to the word using findIndex
- if it exists increment the count, otherwise add a new entry.
Also, so we capture that last word in the sentence without the period (or any commas), we can strip those out with text.replace(/[,.-]/g, '')
. Finally, so we can compare the first word (capitalized) with any matches along the way, we'll convert all comparisons to lowercase.
let text = 'Lorem ipsum lorem ipsum dolor amet sit amet.'
let words = text.replace(/[,.-]/g, '').split(" ");
let array = [];
for (let i = 0; i < words.length; i ) {
let exists = array.findIndex(a => a.word.toLowerCase() === words[i].toLowerCase());
if (exists > -1) array[exists].count ;
else array.push({
word: words[i],
count: 1
});
}
console.log(array);
CodePudding user response:
To access properties in the first code, you can use a for loop to iterate over properties of the obj which contains count like:
let text = 'Lorem ipsum dolor sit amet.'
let words = text.split(" ");
let obj = {};
let i = 0;
let len = words.length
for (i = 0; i < len; i ) {
if (obj[words[i]] === undefined) {
obj[words[i]] = 1;
} else {
obj[words[i]] ;
}
}
console.log(obj);
for (const [key, value] of Object.entries(obj)) {
console.log(`${key}: ${value}`);
}
Of course this is just an example, but I believe this should get the job done for you.
CodePudding user response:
You can acces individual values by calling obj['Lorem']
for example it's a key value map.