2017-09-19 2 views
-1

J'ai un tableau commeSplit et trouver la valeur en utilisant JavaScript EXISTE

[test1^^^68936^^^2017-06-05^^^274^^^NO^^^location1^^^1840, test2^^^68766^^^2016-06-05^^^274^^^NO^^^location2^^^1840,test3^^^68966^^^2017-06-05^^^274^^^YES^^^location1^^^1840] 

où je dois diviser et savoir si la quatrième clé dans ce tableau de clés a NON ou OUI ou 1. Si les deux NO et YES existent dans un tableau, j'ai besoin d'alerter l'utilisateur qu'ils ont tous les deux dedans.

s'il vous plaît laissez-moi savoir comment faire en JavaScript.

+0

En dehors de vous montrer aucun effort absolu, cela ne ne semble pas être très différent de votre question précédente, https://stackoverflow.com/questions/46013087/split-using-jquery-loop-through-the-string – CBroe

+0

Kiran était la réponse utile? Veuillez répondre –

+0

@AKA, j'ai utilisé votre partie de code et l'ai terminée. Mon code est collé dans les commentaires. Merci mec. –

Répondre

0

Vous pouvez y parvenir sans split() aussi:

var array = ['test1^^^68936^^^2017-06-05^^^274^^^NO^^^location1^^^1840', 'test2^^^68766^^^2016-06-05^^^274^^^NO^^^location2^^^1840','test3^^^68966^^^2017-06-05^^^274^^^YES^^^location1^^^1840']; 
 

 
var arrayStr = array.toString(); 
 
if(arrayStr.indexOf('^^^YES') !== -1 && arrayStr.indexOf('^^^NO') !== -1){ 
 
    alert('You have both of them in it'); 
 
}else{ 
 
    alert('You do not have both of them in it'); 
 
}

0

En supposant que votre tableau contient des chaînes

["test1^^^68936^^^2017-06-05^^^274^^^NO^^^location1^^^1840", "test2^^^68766^^^2016-06-05^^^274^^^NO^^^location2^^^1840","test3^^^68966^^^2017-06-05^^^274^^^YES^^^location1^^^1840"] 

Vous pouvez obtenir le OUI/NON clés par la cartographie du tableau et le divisant comme ça

["test1^^^68936^^^2017-06-05^^^274^^^NO^^^location1^^^1840", "test2^^^68766^^^2016-06-05^^^274^^^NO^^^location2^^^1840","test3^^^68966^^^2017-06-05^^^274^^^YES^^^location1^^^1840"].map(function(str){return str.split("^^^")[4]}) 
0

Essayez cette

res = arr.join("").match(/NO|YES/g) 
if(res.includes("YES") && res.includes("NO")){ 
    console.log("Both present") 
} 
0

J'ai essayé mais j'ai résolu

var arrayStr = myarray.toString(); 
var stringExists = []; 

if(arrayStr.indexOf('^^^NO^^^') !== -1) { 
    stringExists.push('0'); 
} 
if(arrayStr.indexOf('^^^YES^^^') !== -1) { 
    stringExists.push('1'); 
} 

if (stringExists.length > 1) { 
    alert("Error"); 
    return; 
} 

Merci pour tous vos gars de soutien ..