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

127
Vistas
How to filter data of object according to keys and make new array in react?

I am working with the object like this:-

{
 0: 
  buyAmount: 16.664328043964396
  buyAmountInUsd: 16.685266204095775
  date: {date: '2021-12-07'}
  sellAmount: {USD: 500, INR: 35000}
 1:
  buyAmount: 1004.7015442959262
  buyAmountInUsd: 1005.9639175379324
  date: {date: '2021-12-07'}
  sellAmount: {USD: 1000, INR: 79000}
......

and I am trying to make a new array using useState hook with this data but the problem I am facing is how to filter data and make an almost similar array with the same data.

e.g.:-

 0: [amount: 500, date: '2021-12-07'], 
 1: [amount: 1000, date: '2021-12-07']

The problem I am facing is I don't know the approach how to get the data like amount = sellAmount.USD and date = date.date

I thought of trying the for...of But I don't think it will be a good hit.

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

0

You can do this with Array.map

const arr = [{
  buyAmount: 16.664328043964396,
  buyAmountInUsd: 16.685266204095775,
  date: {date: '2021-12-07'},
  sellAmount: {USD: 500, INR: 35000}
},{
  buyAmount: 1004.7015442959262,
  buyAmountInUsd: 1005.9639175379324,
  date: {date: '2021-12-07'},
  sellAmount: {USD: 1000, INR: 79000}
}]

console.log(
  arr.map(initialValue => {
    return {
      amount: initialValue.sellAmount.USD,
      date: initialValue.date.date
    }
  })
)

about 4 years ago · Juan Pablo Isaza Denunciar

0

The better idea would be the having an array of objects rather than array of array elements

let result = yourArray.map(each => ({ amount: each.sellAmount.USD, date: each.date.date }))
about 4 years ago · Juan Pablo Isaza Denunciar

0

This is not related to ReactJS - just native Javascript object handling.

If your original data has the shape of an Object you can convert it into an Array like so:

const objData = { 0: {...}, 1: {...} };
const arrData = Object.entries(objData); // [[0, {...}], [1, {...}]];

From there, you can filter/map/sort your array with native array methods:

const reshapedArray = arrData.map(([key, value]) => {
  return {
    amount: value.sellAmount.USD,
    date: value.date.date,
  };
});

Then sort:

const sortedArray = reshapedArray.sort((prev, next) => {
  return prev.amount - next.amount; // sort by amount ascending
});

You can of could chain these array functions and shorten the syntax a bit:

Object.entries(objData)
  .map(([_, { sellAmount, date: { date } }]) => ({ amount: sellAmount.USD, date }))
  .sort((a, b) => a.amount - b.amount);
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