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

153
Views
Combine objetos con la misma clave en 2 matrices en una matriz en Javascript/React

Tengo dos matrices de objetos que quiero fusionar en una sola matriz y fusionar sus objetos también.

Las dos matrices:

 const itemsFr = [ { title: "Bonjour" }, { title: "Bonne Nuit" } ]; const itemsEn = [ { title: "Good Morning" }, { title: "Good Night" } ];

El resultado que quiero:

 items = [ { title: { fr: "Bonjour", en: "Good Morning" } }, { title: { fr: "Bonne Nuit", en: "Good Night" } } ];

Esto es lo que probé:

 export default function App() { const [items, setItems] = useState([]); const itemsFr = [ { title: "Bonjour" }, { title: "Bonne Nuit" } ]; const itemsEn = [ { title: "Good Morning" }, { title: "Good Night" } ]; const fillItemsFrArray = () => itemsFr.map((item) => { setItems(...items, { title: { fr: item.title } }); }); const fillItemsEnArray = () => itemsEn.map((item) => { setItems(...items, { title: { en: item.title } }); }); useEffect(() => { fillItemsFrArray(); fillItemsEnArray(); }, []); return ( <div className="App"> <h2>{JSON.stringify(items, null, 4)}</h2> </div> ); }

Aquí está el enlace de CodeSandbox: https://codesandbox.io/s/restless-night-xq6qg?file=/src/App.js:670-1166

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

0

esto funcionara?

 const itemsFr = [ { title: "Bonjour" }, { title: "Bonne Nuit" }, { title: "bla bla" } ]; const itemsEn = [ { title: "Good Morning" }, { title: "Good Night" } ]; const items = itemsFr.map((i,c) => ({fr:i.title, en:itemsEn[c]?.title})); console.log(items)

about 4 years ago · Juan Pablo Isaza Report

0

Puede adoptar un enfoque dinámico utilizando un objeto con las claves deseadas para el resultado final.

 const itemsFr = [{ title: "Bonjour" }, { title: "Bonne Nuit" }], itemsEn = [{ title: "Good Morning" }, { title: "Good Night" }], result = Object .entries({ fr: itemsFr, en: itemsEn }) .reduce((r, [key, a]) => a.map(({ title }, i) => ({ title: { ...r[i]?.title, [key]: title } })), []); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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!