Estoy usando reaccionar construyendo un comercio electrónico. Estoy obteniendo datos de la API y la configuración es tan peligrosamente SetInnerHTML en JSX. Necesito eliminar algún texto/palabra de los datos de la API. Agregué una foto con Mark.
¿Cómo elimino eso?
with(document)with(body)with(insertBefore(createElement("script"),firstChild))setAttribute("exparams","userid=&aplus&ali_beacon_id=&ali_apache_id=&ali_apache_track=&ali_apache_tracktmp=&dmtrack_c={}&hn=aeproductsourcesite033001217071%2eus44&asid=AQAAAADkyhRi8NcdZwAAAABHROT4NDi+yA==&sidx=Fzc+BeTKFGIAAAAA0uvd1jNUkicntMPM",id="beacon-aplus",src="//assets.alicdn.com/g/alilog/??aplus_plugin_aefront/index.js,mlog/aplus_v2.js") Package included:1 Dress Material:100% Cotton Colors:Beige,Navy,Light Green,Pink Size:S,M,L,XL,2XL,,, Sleeve Length:Full Sleeve Neckline:Square Neck Pattern: Solid Color Length:Ankle Length Thickness:Thin Decoration:Side Pockets Style:Leisure,Bohemian,Retro,Europe Season:Spring,Summer,Autumn Occasion:Family,Holiday,Employment,Travel
Necesito eliminar la primera parte del texto:
ingrese la descripción de la imagen aquí
Imagen: https://prnt.sc/RNl4TG6habCe
Una opción es que puede usar expresiones regulares con el método split()
o con el método match()
en su lugar. La expresión regular variará según el método que elija utilizar.
const data = `with(document)with(body)with(insertBefore(createElement("script"),firstChild))setAttribute("exparams","userid=&aplus&ali_beacon_id=&ali_apache_id=&ali_apache_track=&ali_apache_tracktmp=&dmtrack_c={}&hn=aeproductsourcesite033001217071%2eus44&asid=AQAAAADkyhRi8NcdZwAAAABHROT4NDi+yA==&sidx=Fzc+BeTKFGIAAAAA0uvd1jNUkicntMPM",id="beacon-aplus",src="//assets.alicdn.com/g/alilog/??aplus_plugin_aefront/index.js,mlog/aplus_v2.js") Package included:1 Dress Material:100% Cotton Colors:Beige,Navy,Light Green,Pink Size:S,M,L,XL,2XL,,, Sleeve Length:Full Sleeve Neckline:Square Neck Pattern: Solid Color Length:Ankle Length Thickness:Thin Decoration:Side Pockets Style:Leisure,Bohemian,Retro,Europe Season:Spring,Summer,Autumn Occasion:Family,Holiday,Employment,Travel`; const removeWithSplit = (data) => data.split(/(\n|\r){2,}/).pop(); const removeWithMatch = (data) => data.match(/(\w\s?)+:.*/g)?.join('\n'); console.log('**WITH SPLIT**'); console.log(removeWithSplit(data), '\n'); console.log('**WITH MATCH**'); console.log(removeWithMatch(data), '\n');
Prima:
Es un fastidio que la API devuelva este texto sin formato. ¿Es posible recuperar JSON en su lugar? Si no, aquí hay una forma de convertir sus resultados en un objeto:
const data = `with(document)with(body)with(insertBefore(createElement("script"),firstChild))setAttribute("exparams","userid=&aplus&ali_beacon_id=&ali_apache_id=&ali_apache_track=&ali_apache_tracktmp=&dmtrack_c={}&hn=aeproductsourcesite033001217071%2eus44&asid=AQAAAADkyhRi8NcdZwAAAABHROT4NDi+yA==&sidx=Fzc+BeTKFGIAAAAA0uvd1jNUkicntMPM",id="beacon-aplus",src="//assets.alicdn.com/g/alilog/??aplus_plugin_aefront/index.js,mlog/aplus_v2.js") Package included:1 Dress Material:100% Cotton Colors:Beige,Navy,Light Green,Pink Size:S,M,L,XL,2XL,,, Sleeve Length:Full Sleeve Neckline:Square Neck Pattern: Solid Color Length:Ankle Length Thickness:Thin Decoration:Side Pockets Style:Leisure,Bohemian,Retro,Europe Season:Spring,Summer,Autumn Occasion:Family,Holiday,Employment,Travel`; const removeWithMatch = (data: string) => data.match(/(\w\s?)+:.*/g)?.join('\n'); const resultsToObject = (data: string) => { return data.split('\n').reduce((acc, curr) => { const [key, value] = curr.split(':'); return { ...acc, [key]: value, }; }, {} as Record<string, unknown>); }; console.log(resultsToObject(removeWithMatch(data) as string));