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?
write a javascript function called cal_modes that will take a List of numbers or strings as...
write a javascript function called cal_modes that will take a List of numbers or strings as input and returns a List of the most frequent values. If there's only one most-frequent value, it returns a single-element List. example cal_modes([3,4,5,5]) should return [5] cal_modes([8.9, 1, 1]) should return [1] cal_modes([2.5, -2, -2, 2.5]) should return [2.5] cal_modes([3,3,4,4]) should return [3,4] cal_modes([3,4,5]) should return [3,4,5], because all occur with equal frequency cal_modes(["hi", "what", "where", "hi"]) should return ["hi”]
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...
I am trying to tokenize a string using a function by passing the char string[] and...
I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function. I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]). This is my code below: int main() {    // Declare variables    char str[] = "this is a test only...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT