• Jobs
  • About Us
  • Jobs
    • Home
    • Jobs
    • Courses and challenges
  • Businesses
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

260
Views
¿Cómo filtrar para múltiples condiciones con JavaScript?

En el siguiente código de ejemplo, ¿cómo puedo filtrar solo aquellas filas de "Vender" en el elemento "Estado" y más del 30 % en el elemento "Beneficio" de la variable "datos", usando la variable "criterios"? ¿Alguien puede ayudarme a terminar el código? Solía usar las funciones de filtro de Hojas de cálculo de Google, SpreadsheetApp.nuevos criterios de filtro().whenNumber..., pero me tomó mucho tiempo (5 minutos) actualizar mis Hojas de cálculo de Google, ya que mi aplicación real tiene 3000 filas y 140 columnas para filtrar y necesitaba tantas llamadas. Más bien, aprendo cómo hacer operaciones de filtrado dentro del nivel de JavaScript, esperando que sea mucho más rápido. Luego puedo enviar los datos filtrados a Hojas de cálculo de Google. ¡Gracias por cualquier ayuda!

 function test() { var data = [['Product', 'Status', 'Price', 'Profit'], ['Apple', 'Hold', 10, '10%'], ['Mango', 'Sell', 20, '20%'], ['Orange', 'Buy', 30, '30%'], ['Juice', 'Sell', 40, '40%'], ['Coffee', 'Sell', 50, '50%']]; data.splice(0, 1); // Remove header var criteria = ['', 'Sell', '', '>30%']; var filteredData = data; for (var i = 0; i < criteria.length; ++i) { if (criteria[i]) { filteredData = filteredData.filter(???); console.log(filteredData); } } }
over 3 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede iterar todos los criterios junto con sus valores de columna y verificar el valor o calcular el valor porcentual.

 const criteria = ['', 'Sell', '', '>30%'], data = [['Product', 'Status', 'Price', 'Profit'], ['Apple', 'Hold', 10, '10%'], ['Mango', 'Sell', 20, '20%'], ['Orange', 'Buy', 30, '30%'], ['Juice', 'Sell', 40, '40%'], ['Coffee', 'Sell', 50, '50%']], result = data .slice(1) .filter(a => criteria.every((c, i) => { if (!c || c === a[i]) return true; if (c.slice(-1) === '%') return eval(a[i].slice(0, -1) + c.slice(0, -1)); })); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

over 3 years ago · Juan Pablo Isaza Report

0

function test() { var data = [['Product', 'Status', 'Price', 'Profit'], ['Apple', 'Hold', 10, '10%'], ['Mango', 'Sell', 20, '20%'], ['Orange', 'Buy', 30, '30%'], ['Juice', 'Sell', 40, '40%'], ['Coffee', 'Sell', 50, '50%']]; data.shift(); let fdata = data.filter(r => r[1] == 'Sell' && parseInt(r[3]) > 30); Logger.log(fdata.join('\n')); } 1:45:07 PM Notice Execution started 1:45:08 PM Info Juice,Sell,40,40% Coffee,Sell,50,50% 1:45:08 PM Notice Execution completed
over 3 years ago · Juan Pablo Isaza Report

0

.filter , .map , .reduce , .forEach y otras funciones similares,
tiene algunas opciones con respecto a los parámetros que puede pasar, puede leer más al respecto aquí .

Para este caso de uso, en realidad no necesita ningún criteria ,
simplemente puedes hacer:

 function test() { var data = [['Product', 'Status', 'Price', 'Profit'], ['Apple', 'Hold', 10, '10%'], ['Mango', 'Sell', 20, '20%'], ['Orange', 'Buy', 30, '30%'], ['Juice', 'Sell', 40, '40%'], ['Coffee', 'Sell', 50, '50%']]; data.splice(0, 1); // Remove header var filteredData = data; filteredData = filteredData.filter(currentData => { if (currentData[1] === 'Sell' && parseInt(currentData[3]) > 30) { return currentData; } }); }

Una vez hecho esto, filteredData se llena con las matrices 'Juice' y 'Coffee' en este caso, y luego puede hacer lo que necesite con él.

over 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Show me some job opportunities
There's an error!