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

0

195
Views
Encuentre todos los índices según la condición

¿Cómo puedo obtener todos los índices en función de una condición para una matriz de objetos? Probé el código a continuación, pero solo devuelve la primera aparición.

 a = [ {prop1:"abc",prop2:"yutu"}, {prop1:"bnmb",prop2:"yutu"}, {prop1:"zxvz",prop2:"qwrq"}]; index = a.findIndex(x => x.prop2 ==="yutu"); console.log(index);

over 3 years ago · Santiago Trujillo
6 answers
Answer question

0

Prueba Array.reduce

 a = [ {prop1:"abc",prop2:"yutu"}, {prop1:"bnmb",prop2:"yutu"}, {prop1:"zxvz",prop2:"qwrq"}]; index = a.reduce((acc, {prop2}, index) => prop2 ==="yutu" ? [...acc, index] : acc, []); console.log(index);

over 3 years ago · Santiago Trujillo Report

0

findIndex devolverá solo un índice coincidente. Puede verificar el valor con la propiedad prop2 usando el filter

 a = [ {prop1:"abc",prop2:"yutu"}, {prop1:"bnmb",prop2:"yutu"}, {prop1:"zxvz",prop2:"qwrq"}]; const allIndexes = a .map((e, i) => e.prop2 === 'yutu' ? i : -1) .filter(index => index !== -1); console.log(allIndexes); // This is one liner solution might not work in older IE ('flatMap') const notSupportedInIE =a.flatMap((e, i) => e.prop2 === 'yutu' ? i : []); console.log(notSupportedInIE);

over 3 years ago · Santiago Trujillo Report

0

Puede usar normal for loop y cuando alguna vez prop2 coincida, empuje el índice en la matriz

 const a = [{ prop1: "abc", prop2: "yutu" }, { prop1: "bnmb", prop2: "yutu" }, { prop1: "zxvz", prop2: "qwrq" } ]; const indArr = []; for (let i = 0; i < a.length; i++) { if (a[i].prop2 === 'yutu') { indArr.push(i) } } console.log(indArr);

over 3 years ago · Santiago Trujillo Report

0

El método findIndex devuelve el índice del primer elemento de la matriz que satisface la función de prueba proporcionada. De lo contrario, devuelve -1, lo que indica que ningún elemento pasó la prueba. - MDN

Puedes usar reduce aquí:

 const a = [ { prop1: "abc", prop2: "yutu" }, { prop1: "bnmb", prop2: "yutu" }, { prop1: "zxvz", prop2: "qwrq" }, ]; const result = a.reduce((acc, curr, i) => { if (curr.prop2 === "yutu") acc.push(i); return acc; }, []); console.log(result);

over 3 years ago · Santiago Trujillo Report

0

Simplemente puede iterar a través de objetos, por ejemplo

 function getIndexes(hystack, nameOfProperty, needle) { const res = new Array(); for (const [i, item] of hystack.entries()) { if (item[nameOfProperty] === needle) res.push(i); } return res; } const items = [ {prop1:"a", prop2:"aa"}, {prop1:"b", prop2:"bb"}, {prop1:"c", prop2:"aa"}, {prop1:"c", prop2:"bb"}, {prop1:"d", prop2:"cc"} ]; const indexes = getIndexes(items, 'prop2', 'bb'); console.log('Result', indexes);

over 3 years ago · Santiago Trujillo Report

0

Puede usar directamente el filter sin la función de mapa

 const a = [ { prop1: "abc", prop2: "yutu" }, { prop1: "bnmb", prop2: "yutu" }, { prop1: "zxvz", prop2: "qwrq" }, ]; const res = a.filter((item) => { return item.prop2==="yutu"; }); console.log(res);
over 3 years ago · Santiago Trujillo 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