Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
¿Cómo agregar una nueva entrada a una matriz después de que cambie cierta propiedad?

Estoy tratando de agregar una nueva entrada a una matriz después de que una determinada propiedad, en mi caso, "lugar", cambie. Sin embargo, tengo grandes problemas para encontrar un enfoque adecuado, porque la operación depende del siguiente objeto de la matriz.

 const initalData = [ { storyID: 1, place: 12, type: "Story" }, { storyID: 5, place: 12, type: "Story" }, { storyID: 99, place: 45, type: "Story" }, { storyID: 8, place: 31, type: "Story" }, { storyID: 16, place: 31, type: "Story" }, { storyID: 20, place: 45, type: "Story" }, { storyID: 22, place: 45, type: "Story" }, ];
 const targetData = [ { placeID: 12, type: "Place" }, // new added entry based on the next "place" { storyID: 1, place: 12, type: "Story" }, { storyID: 5, place: 12, type: "Story" }, { placeID: 45, type: "Place" }, // // new added entry based on the next "place" { storyID: 99, place: 45, type: "Story" }, { placeID: 31, type: "Place" }, // new added entry based on the next "place" { storyID: 8, place: 31, type: "Story" }, { storyID: 16, place: 31, type: "Story" }, { placeID: 45, type: "Place" },// new added entry based on the next "place" { storyID: 20, place: 45, type: "Story" }, { storyID: 22, place: 45, type: "Story" }, ];

Actualmente he mirado groupBy("place") como un posible enfoque, pero la estructura de matriz final aquí no es lo que necesito.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Aquí está la implementación

 const initalData = [ { storyID: 1, place: 12, type: "Story" }, { storyID: 5, place: 12, type: "Story" }, { storyID: 99, place: 45, type: "Story" }, { storyID: 8, place: 31, type: "Story" }, { storyID: 16, place: 31, type: "Story" }, { storyID: 20, place: 45, type: "Story" }, { storyID: 22, place: 45, type: "Story" }, ]; let oldPlace = null; let resultData = []; for (let row of initalData) { if (row.place != oldPlace) { resultData.push({ placeID: row.place, type: "Place" }); oldPlace = row.place; } resultData.push(row); } console.log (resultData);

ingrese la descripción de la imagen aquí

about 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!