-1

I searched on Google for the past 2 hours and been using all kind of codes, even tried XML php (which turned out to be slow). I still haven't found a solution!

Here's what I want: I need to search through this JSON file https://s2.coinmarketcap.com/generated/search/quick_search.json with jQuery or javascript, whichever is fastest. I want to search for "name" and "symbol" with a string specified by me.

Upon searching, a total of 5 results will be logged into console. When it's logged into console, it must tell me the "name" and "rank" value of the result.

Can someone please help me?

R Records
  • 17
  • 4

2 Answers2

0

Based on the assumption you are already able to fetch the data (quickSearchResults), a parsing function would look like the following:

function findElementInData(dataPoints, nameOrSymbol) {
    return dataPoints.filter(point => point.name == nameOrSymbol || point.symbol === nameOrSymbol)
                     .map(point => {
                         return { name: point.name, rank: point.rank }
                      });
}
var elementsByName = findElementInData(quickSearchResults, 'Bitcoin');
console.log(elementsByName); // [{name: "Bitcoin", rank: 1} ]
var elementsBySymbol = findElementInData(quickSearchResults, 'BTC');
console.log(elementsBySymbol); // [{name: "Bitcoin", rank: 1} ] 

This will give you all elements matching either the name or the symbol passed for the given array of data.

Update: It appears you do not have access to the data yet. This is a full example:

function findElementInData(dataPoints, nameOrSymbol) {
    return dataPoints.filter(point => point.name === nameOrSymbol || point.symbol === nameOrSymbol)
                .map(point => {
                    return { name: point.name, rank: point.rank }
                });
    }

function getData(dataUrl) {
    return fetch(dataUrl)
    .then(res => res.json())
}

window.addEventListener('load', () => {
    getData('https://s2.coinmarketcap.com/generated/search/quick_search.json')
    .then(dataPoints => {
        console.log(dataPoints);
        let myResultsBTC = findElementInData(dataPoints, 'BTC');
        let myResultsBitcoin = findElementInData(dataPoints, 'Bitcoin');
        let myResultsEtherum = findElementInData(dataPoints, 'Ethereum');
        let myResultsETH = findElementInData(dataPoints, 'ETH');
        console.log(myResultsBTC);       // {name: "Bitcoin", rank: 1}
        console.log(myResultsBitcoin);  // {name: "Bitcoin", rank: 1}
        console.log(myResultsEtherum);  // {name: "Ethereum", rank: 2}
        console.log(myResultsETH);      // {name: "Ethereum", rank: 2}
    })
    .catch(err => {
        console.error(err);
    });
});
William Fleming
  • 466
  • 2
  • 7
  • That seems too specific. Can you please make it so if the search string contains the symbol or name? – R Records Mar 03 '18 at 22:28
  • If I am understanding correctly: For any given element in that JSON document, if the name OR symbol property matches your search criteria, it should be returned. That is how the above operates. You can call it with either the name of the currency or its symbol. – William Fleming Mar 03 '18 at 22:56
  • The code works. Can you please change it so that the data is loaded on page load, so it doesn't have to load again everytime I search? – R Records Mar 03 '18 at 23:45
  • That edit will allow you to search each time without reloading the data. If you are going to be using this outside of a hobby project, ensure you have permission to use their API. If this solves your problem, please mark the answer as correct. – William Fleming Mar 04 '18 at 00:10
  • For some reason, it returns the result two times. I removed "|| point.symbol === nameOrSymbol" then it only showed once. – R Records Mar 04 '18 at 00:11
  • Hmm, that is odd, edited it with what the output should be for each example search. There is some more you could do to improve this, but this should be a good starting point. – William Fleming Mar 04 '18 at 00:18
0

Yeah so if you use PHP, here is what I would do:

  1. Load the JSON file into a variable using file_get_contents, and json_decode it.
  2. Iterate through the variable using a simple foreach loop.
  3. Use strpos to check if one of the 2 keys contains a value I'm searching for.

You can create another array or object and dump the successfully searched items into it.

This would be lightning fast.

kjdion84
  • 9,552
  • 8
  • 60
  • 87