Search code examples
javascriptjsonnode.jsalexa-voice-service

How to compare two String Source and destination with fullname in nodejs


Compare two string with third-string like JSON Array full name

var source = intentObj.slots.toPlazaName.value.toString(); // Jaipur 
var destination = intentObj.slots.fromPlazaName.value.toString(); // Kishangarh

match with this "FullName": "Jaipur - Kishangarh Section",

My response

{
            "projectid": 10,
            "FullName": "Kishangarh -Ajmer- Beawar",
            "piu": "PIU-Ajmer",
            "NumberofLanes": "6L",
            "NHNo_New": "8",
            "NHNo_Old": "8",
            "Total_Length": 92,
            "state_name": "Rajasthan"
        },
        {
            "projectid": 15,
            "FullName": "Reengus - Sikar",
            "piu": "PIU-Sikar",
            "NumberofLanes": "4L",
            "NHNo_New": "52",
            "NHNo_Old": "11",
            "Total_Length": 44,
            "state_name": "Rajasthan"
        },
        {
            "projectid": 20,
            "FullName": "Rajsamand - Gangapur - Bhilwara",
            "piu": "PIU-Chittorgarh",
            "NumberofLanes": "4L",
            "NHNo_New": "758",
            "NHNo_Old": "76B",
            "Total_Length": 87.25,
            "state_name": "Rajasthan"
        },

my create function

function findSourceDestination(source, destination, callback) {
    var matchPlazas = [];
    var projectFullName = [];

    for (var i = 0; i < nhai_response.GetALexDataInJSONResult.length; i++) {
        // console.log(nhai_response.GetALexDataInJSONResult[i]["FullName"]);
        if (nhai_response.GetALexDataInJSONResult[i]["FullName"].includes(source)) {
            // console.log("source " + nhai_response.GetALexDataInJSONResult[i]["FullName"]);
            projectFullName.push(nhai_response.GetALexDataInJSONResult[i]);
        }
    }

    for (var j = 0; j < projectFullName.length; j++) {
        if (projectFullName[j]["FullName"].includes(destination)) {
            console.log('----------------' +projectFullName[j]["FullName"] + '----------destination '+ destination +'------------');
            matchPlazas.push(projectFullName[j]);
        } 

    }

    callback(matchPlazas);
}

I have full name string another. I have two another string. I want to match source string with my full name first-word destination will match or contain with full name, not the first string like the source string.

Please help me.


Solution

  • If I understood well your questions, and you need the source to be the first part of FullName, and destination needs to be in the rest of FullName, this function makes it

    function findSourceDestination(source, destination, callback) {
        var matchPlazas = [];
        var projectFullName = [];
    
        for (var i = 0; i < nhai_response.GetALexDataInJSONResult.length; i++) {
            let fullName = nhai_response.GetALexDataInJSONResult[i]["FullName"];
            let fullNameParts = fullName.split("-");
            if (fullNameParts[0].trim().includes(source) && fullName.includes(fullName)) {
                matchPlazas.push(nhai_response.GetALexDataInJSONResult[i]);
            }
        }
    
        callback(matchPlazas);
    }