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

0

218
Views
¿Cómo usar una matriz de cadenas para filtrar una matriz de objetos?

Dada una serie de objetos:

 people = [ {id: "1", name: "abc", gender: "m", age:"15", country:"USA" }, {id: "2", name: "def", gender: "m", age:"25", country:"BRA" }, {id: "3", name: "ghi", gender: "f", age:"05", country:"CHI" }, {id: "4", name: "jkl", gender: "m", age:"35", country:"RUS" }, {id: "5", name: "mno", gender: "m", age:"41", country:"JAP" }, {id: "6", name: "pqr", gender: "f", age:"30", country:"COL" }, {id: "7", name: "stu", gender: "f", age:"31", country:"CAN" }, {id: "8", name: "vwx", gender: "m", age:"78", country:"USA" }, ]

Y una serie de claves buscadas:

 wantedKeys = ["name", "age", "country"]

Rendimiento esperado:

 peopleFiltered = [ {name: "abc", age:"15", country:"USA" }, {name: "def", age:"25", country:"BRA" }, {name: "ghi", age:"05", country:"CHI" }, {name: "jkl", age:"35", country:"RUS" }, {name: "mno", age:"41", country:"JAP" }, {name: "pqr", age:"30", country:"COL" }, {name: "stu", age:"31", country:"CAN" }, {name: "vwx", age:"78", country:"USA" }, ]

¿Cómo filtrar la matriz de people para devolver una nueva matriz de objetos solo con los elementos contenidos en la matriz de wantedKeys ?

about 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Solución

Probablemente haya una forma aún más corta con reduce , pero este debería ser un punto de partida válido.

 var peopleFiltered = people.map(person => { var obj = {}; wantedKeys.forEach((key) => obj[key] = person[key]); return obj; });

Demostración de trabajo

 people = [{ id: "1", name: "abc", gender: "m", age: "15", country: "USA" }, { id: "2", name: "def", gender: "m", age: "25", country: "BRA" }, { id: "3", name: "ghi", gender: "f", age: "05", country: "CHI" }, { id: "4", name: "jkl", gender: "m", age: "35", country: "RUS" }, { id: "5", name: "mno", gender: "m", age: "41", country: "JAP" }, { id: "6", name: "pqr", gender: "f", age: "30", country: "COL" }, { id: "7", name: "stu", gender: "f", age: "31", country: "CAN" }, { id: "8", name: "vwx", gender: "m", age: "78", country: "USA" }, ] wantedKeys = ["name", "age", "country"] var peopleFiltered = people.map(person => { var obj = {}; wantedKeys.forEach((key) => obj[key] = person[key]); return obj; }); console.log(peopleFiltered);

about 3 years ago · Juan Pablo Isaza Report

0

Hay una función lodash muy útil, elija que hace exactamente esto. Podemos combinar con Array.map() para obtener el resultado deseado:

 people = [ {id: "1", name: "abc", gender: "m", age:"15", country:"USA" }, {id: "2", name: "def", gender: "m", age:"25", country:"BRA" }, {id: "3", name: "ghi", gender: "f", age:"05", country:"CHI" }, {id: "4", name: "jkl", gender: "m", age:"35", country:"RUS" }, {id: "5", name: "mno", gender: "m", age:"41", country:"JAP" }, {id: "6", name: "pqr", gender: "f", age:"30", country:"COL" }, {id: "7", name: "stu", gender: "f", age:"31", country:"CAN" }, {id: "8", name: "vwx", gender: "m", age:"78", country:"USA" }, ] wantedKeys = ["name", "age", "country"]; let result = people.map(p => _.pick(p, wantedKeys)); console.log('Result:'); result.forEach(r => console.log(JSON.stringify(r)))
 .as-console-wrapper { max-height: 100% !important; top: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" referrerpolicy="no-referrer"></script>

También un enfoque de JavaScript estándar, usando Object.fromEntries() y Array.map() :

 people = [ {id: "1", name: "abc", gender: "m", age:"15", country:"USA" }, {id: "2", name: "def", gender: "m", age:"25", country:"BRA" }, {id: "3", name: "ghi", gender: "f", age:"05", country:"CHI" }, {id: "4", name: "jkl", gender: "m", age:"35", country:"RUS" }, {id: "5", name: "mno", gender: "m", age:"41", country:"JAP" }, {id: "6", name: "pqr", gender: "f", age:"30", country:"COL" }, {id: "7", name: "stu", gender: "f", age:"31", country:"CAN" }, {id: "8", name: "vwx", gender: "m", age:"78", country:"USA" }, ] wantedKeys = ["name", "age", "country"]; let result = people.map(p => Object.fromEntries(wantedKeys.map(k => [k,p[k]]))); console.log('Result:'); result.forEach(r => console.log(JSON.stringify(r)))
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 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

Recommend me some offers
I have an error