0
I need help to do a function where I put a code to search and it shows me the result. The problem is that it currently only brings the result if I put it exactly as it is in JSON.
Ex.: if I search "9 190 087 006" it will bring me the line that is this code, but if I put the same code "9190087006" (no spaces), it does not find.
Can help me improve the code so that regardless of any special character it can bring the results, considering only the numbers and letters to do the search?
<textarea id='txt' style="height: 200">
9 190 087 006
9190087006
13067/5
130675
VR-B190
VR B190
</textarea>
<br>
<button onclick='limpar()'>Clear</button>
<button onclick='render()'>Pesquisa</button>
<!-- <input type="" name="" id='pesq'> -->
<div id="lista"></div>
<script>
x = [
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"9 190 087 006"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"9-190-085-001"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"9.190.087.011"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"13067/5"}},
{"Codigo":"T001","Marca":"AAAAAA","Cod":{" Marca":"2500216"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"130216"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"IK420HD"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"VR-B190"}},
{"Codigo":"T002","Marca":"AAAAAA","Cod":{" Marca":"VRB190"}},
{"Codigo":"T003","Marca":"AAAAAA","Cod":{" Marca":"IB305"}},
{"Codigo":"T003","Marca":"AAAAAA","Cod":{" Marca":"9 190 083 002"}},
{"Codigo":"T003","Marca":"AAAAAA","Cod":{" Marca":"1197 BE3 002"}}
]
function pesquisa(valor){
// valor = this.value
resultados = []
for(i of x){
reg = new RegExp(valor,"g")
if(valor == i.Cod[' Marca'].match(reg)){
resultados.push(i)
}
}
return resultados
// console.log(resultados)
// lista.innerHTML = resultados
}
function add(str){
j = []
json1 = pesquisa(str)
div = document.createElement("div")
div.innerHTML = JSON.stringify(json1)
lista.appendChild(div)
}
function render(){
t = txt.value.split("\n")
for(o of t){
add(o)
}
}
render()
function limpar(){
lista.innerHTML = ""
}
</script>
thanks very much it worked out friend
– Fragosojp