Question

In: Computer Science

I want to compare a string with the previous strings in a forEach loop using javascript....

I want to compare a string with the previous strings in a forEach loop using javascript.

So im accessing data from an API. Im iterating through them using a forEach() loop. I want to compare the current 'title' string from the api with all previous title strings its pulled. The title is located under article.title

fetch(req)

            .then((response) => {

                return response.json();

            })

            .then((data) => {

                console.log(data);

                data.articles.forEach((article) => {

}

If the title matches any other title used already id like to skip the entire iteration.

Solutions

Expert Solution

To do this you have to maintain a list of all the title used so far and in the then part of the fetch request you have to compare with all the elements in fetch data and take action accordingly

Here is the solution

JAVASCRIPT
let titlesUsedSoFar=[] //to maintain a list of titles used so far
fetch(req)

.then((response) => {

return response.json();

})

.then((data) => {

console.log(data);

data.articles.forEach((article) => {
   let flag=false; //flag to check if variable exists
   //for loop to check if the title already pulled from api
   for(let i=0;i<titlesUsedSoFar.length;++i)
   {
       if(titleUsedSoFar===article.title)
       {
           flag=true; //set flag = true if it exists
                       }
                   }
                   if(flag==false) //if doesnt exists
                   {
                       //add in the titles list
                       //DO THE PROCESSING YOU WANT TO DO HERE
                       titlesUsedSoFar.push(article.title);
                   }
                   else
                   {
                       //THIS PART DO WHAT YOU WANT TO DO IF ALREADY TITLE PRESENT IN THE USED LIST
                   }
}

}


Related Solutions

Write a function that counts the colors in a string using JavaScript. String "The quick brown...
Write a function that counts the colors in a string using JavaScript. String "The quick brown fox jumped the blue fence and landed on red paint." This should return the number of colors. The colors you are looking for are "blue, green, brown, gray, black, brown, red, purple".
Using HTML/Javascript (if needed) I want to create a table for a website that pulls data...
Using HTML/Javascript (if needed) I want to create a table for a website that pulls data from another website where the data is being updated on. Example: https://investors.coca-colacompany.com/stock-information/historical-data I cannot just put in the numbers to a table as they will be getting updated daily. So it needs to link the the website elements. DATE OPEN HIGH LOW CLOSE VWAP VOLUME % CHG CHANGE TRADE VAL TOTAL TRADES 2020-10-13 -- -- -- 51.09 -- -- 0.00% -- -- -- 2020-10-12...
c++ I want to flip the string when it's string type. For example, if it is...
c++ I want to flip the string when it's string type. For example, if it is 'apple', I would like to print 'elppa'. how can i do?
How do you write JavaScript codes to randomly select five elements by using a single for-loop?...
How do you write JavaScript codes to randomly select five elements by using a single for-loop? How do you display the results to a html page? var password = [“x”, “y”, “z”, “1”, “2”, “3”, “test”]; Sample output: zx21y
You are using PI control on a temperature loop, and it just does not want to...
You are using PI control on a temperature loop, and it just does not want to stabilise. Any ideas what you might want to set / adjust, in order to stabilize the loop, and get the PV to stop oscillating, and settle down at the Set Point?
Recall that a 5-bit string is a bit strings of length 5, and a bit string...
Recall that a 5-bit string is a bit strings of length 5, and a bit string of weight 3, say, is one with exactly three 1’s. a. How many 5-bit strings are there? b. How many 5-bit strings have weight 0? c. How many 5-bit strings have weight 1? d. How many 5-bit strings have weight 2? e. How many 5-bit strings have weight 4? f. How many 5-bit strings have weight 5? g. How many 5-bit strings have weight...
I want this code to be written in c++. Take a string of length n as...
I want this code to be written in c++. Take a string of length n as an input from the user such that n>=8 and only lower-case letters (a-z) are allowed as valid string characters. Deleting a letter from the string removes all the occurrences of that letter. The objective is to find the longest possible string such that it is left with only two unique letters and no two consecutive characters in a string are the same. If there...
6.6 Parsing strings (Java) (1) Prompt the user for a string that contains two strings separated...
6.6 Parsing strings (Java) (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts)...
Document the arrays, using a chart with columns titled: outer loop, inner loop, i, j, and...
Document the arrays, using a chart with columns titled: outer loop, inner loop, i, j, and x. Finally, draw a picture of each array (after the program portion has executed). (5 pts) int[][] arr1 = new int[5][5]; int[][] arr2 = new int[5][5]; x = 1; for(int i = 0; I < 5; i++) { for(int j = 1; j < 6; j++) { arr1[i][j-1] = x; x++; if(x == 6) x += 2; } } for(int i = 4; i...
14.6. Say that a string x overlaps a string y if there exist strings p, q,...
14.6. Say that a string x overlaps a string y if there exist strings p, q, r such that x = pq and y = qr, with q ̸= λ. For example, abcde overlaps cdefg, but does not overlap bcd or cdab. (a) Draw the overlap relation for the four strings of length 2 over the alphabet {a, b}. (b) Is overlap reflexive? Why or why not? (c) Is overlap symmetric? Why or why not? (d) Is overlap transitive? Why...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT