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

274
Vistas
i want to push that object in a new array where free is greater than 1

I want to use the json data to get an object from the json object where free has value greater than 0 and push that whole object in new array for example i want to push {"free": 1, "used": 0,"total": 0, "usd_price": 0,} in to an array and get a result like this [{"free": 1, "used": 0,"total": 0, "usd_price": 0},{"free": 1, "used": 0,"total": 0, "usd_price": 0}]

{
"success": true,
"messsage": "",
"data": {

    "BIT": {
    "free": 0,
    "used": 0,
    "total": 0,
    "usd_price": 0,
    "usd_val": 0,
    "percent_change_24h": 0,
    "usd_pnl_24h": 0,
    "name": "",
    "logo": "https://www.trailingcrypto.com/assets/img/default-coin.png",
    "portfolio_share": 0
    },

    "BTC": {
    "free": 0,
    "used": 0,
    "total": 0,
    "usd_price": 38400.82298054895,
    "usd_val": 0,
    "percent_change_24h": 0,
    "usd_pnl_24h": 0,
    "name": "Bitcoin",
    "logo": "https://s2.coinmarketcap.com/static/img/coins/128x128/1.png",
    "portfolio_share": 0
    },
    "DOT": {
    "free": 0,
    "used": 0,
    "total": 0,
    "usd_price": 20.07605927484185,
    "usd_val": 0,
    "percent_change_24h": 0,
    "usd_pnl_24h": 0,
    "name": "Polkadot",
    "logo": "https://s2.coinmarketcap.com/static/img/coins/128x128/6636.png",
    "portfolio_share": 0
    }

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

0

What you want to do here is make use of Javascript's filter method to get a sub-array that passes a given condition, which in your case is making sure that value of free is greater than 1. However the filter method can be applied only on an array, but the data you have provided is in the form of an object.

So before applying the filter method, let's convert this object into an array by using Object.values().

Object.values(json.data)

Once you have the data in an array format, we can now apply the filter method to get a relevant subset from the original array (again, which in your case are the element where the value of free is greater than 1)

Object.values(json.data).filter((data) => data.free > 1)

And it should now work! Here is the full working code below

const json = {
  success: true,
  messsage: "",
  data: {
    BIT: {
      free: 0,
      used: 0,
      total: 0,
      usd_price: 0,
      usd_val: 0,
      percent_change_24h: 0,
      usd_pnl_24h: 0,
      name: "",
      logo: "https://www.trailingcrypto.com/assets/img/default-coin.png",
      portfolio_share: 0,
    },

    BTC: {
      free: 0,
      used: 0,
      total: 0,
      usd_price: 38400.82298054895,
      usd_val: 0,
      percent_change_24h: 0,
      usd_pnl_24h: 0,
      name: "Bitcoin",
      logo: "https://s2.coinmarketcap.com/static/img/coins/128x128/1.png",
      portfolio_share: 0,
    },
    DOT: {
      free: 0,
      used: 0,
      total: 0,
      usd_price: 20.07605927484185,
      usd_val: 0,
      percent_change_24h: 0,
      usd_pnl_24h: 0,
      name: "Polkadot",
      logo: "https://s2.coinmarketcap.com/static/img/coins/128x128/6636.png",
      portfolio_share: 0,
    },
  },
};

const free = Object.values(json.data).filter((data) => data.free > 1);

console.log(free)

about 4 years ago · Juan Pablo Isaza Denunciar

0

put objects in array then loop through array and check if free is equal to one then if true push the object inside second array.

let dataFilter = [];
const res = {
  success: true,
  messsage: "",
  data: {
    BIT: {
   free: 0,
  used: 0,
  total: 0,
  usd_price: 0,
  usd_val: 0,
  percent_change_24h: 0,
  usd_pnl_24h: 0,
  name: "",
  logo: "https://www.trailingcrypto.com/assets/img/default-coin.png",
  portfolio_share: 0,
},

BTC: {
  free: 0,
  used: 0,
  total: 0,
  usd_price: 38400.82298054895,
  usd_val: 0,
  percent_change_24h: 0,
  usd_pnl_24h: 0,
  name: "Bitcoin",
  logo: "https://s2.coinmarketcap.com/static/img/coins/128x128/1.png",
  portfolio_share: 0,
},
DOT: {
  free: 1,
  used: 0,
  total: 0,
  usd_price: 20.07605927484185,
  usd_val: 0,
  percent_change_24h: 0,
  usd_pnl_24h: 0,
  name: "Polkadot",
  logo: "https://s2.coinmarketcap.com/static/img/coins/128x128/6636.png",
  portfolio_share: 0,
  },
 },
}

  Object.values(res.data).map((item) => {
   if (item?.free === 1) {
     console.log("ok");
     dataFilter.push(item);
   }
});
// now you have access data
console.log(dataFilter)
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