比较两个字符串和第三个字符串,例如JSON Array全名

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


与此"FullName": "Jaipur - Kishangarh Section",匹配

我的回复

{
            "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"
        },


我的创建功能

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);
}


我还有另一个全名字符串。我还有另外两个字符串。我想用我的全名匹配源字符串,第一个单词目标将匹配或包含全名,而不是第一个字符串,例如源字符串。

请帮我。

最佳答案

如果我很好地理解了您的问题,并且您需要将源作为FullName的第一部分,而目标则必须位于FullName的其余部分中,则可以使用此功能

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);
}

关于javascript - 如何在Node.js中比较两个具有全名的String Source和Destination,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48987310/

10-11 22:09