Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

98
Vistas
How to change an array element based on another array element

Whats the goal?
Two arrays: orders and NewOrders

Check if there is an order with the same order_id on both arrays, than compare the order status, if the order from the NewOrders array has a different status then set orders[i].status = NewOrders[i].status

My current code:

const {orders} = this.state;

const page = this.state.page;
const newOrders= await api.get('/orders?page='+page);

for (let i = 0; i < newOrders.length; i++) {
    orders[newOrders[i].order_id].status = newOrders[i].status;             
}

It doesn't properly work
How can I make this work using ES6?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you are working in React you must not mutate the state manually. Map orders into a new array, based on the required transformations, then use setState() to update the state.

const _orders = orders.map(o => {
    const found = newOrders.find(no => o.order_id === no.order_id)
    return found
        ? { ...o, status: found.status }
        : o
})

this.setState({orders: _orders})

Example

const orders = [
    { order_id: "1", status: "OK" },
    { order_id: "2", status: "NOK" },
]
const newOrders = [
    { order_id: "2", status: "OK" },
    { order_id: "3", status: "OK" },
]

Output

[
    { order_id: "1", status: "OK" },
    { order_id: "2", status: "OK" },
]
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use Array#find.

for (const order of orders) {
    const newOrder = newOrders.find(x => x.order_id === order.order_id);
    if(newOrder) order.status = newOrder.status;
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda