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".
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
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...
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?
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)...
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...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output...
Write a function (in Javascript) called longestMorseCodeWords which takes in an array of strings. The output of longestMorseCodeWords should be an array of the strings that were passed in, but ordered by the length of their Morse Code equivalent in descending order. If the length of Morse Code is equal, order the words alphabetically.For convenience, the full table for the 26 letters of the English alphabet is given below: [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] let words = ["gin", "zen", "gig", "msg"] longestMorseCodeWords(words) The Morse...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT