Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
Merge objects with the same key in 2 arrays into one array in Javascript/React

I have two arrays of objects that I want to merge into a single array and merge their objects too.

The two arrays:

const itemsFr = [
    {
      title: "Bonjour"
    },
    {
      title: "Bonne Nuit"
    }
  ];

  const itemsEn = [
    {
      title: "Good Morning"
    },
    {
      title: "Good Night"
    }
  ];

The result that I want:

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

Here is what I tried:

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>
  );
}

Here is the CodeSandbox link: https://codesandbox.io/s/restless-night-xq6qg?file=/src/App.js:670-1166

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Will this work?

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 Relatório

0

You could take a dynamic approach by using an object with wanted keys fro the final result.

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda