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

268
Visualizações
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 Respostas
Responde à pergunta

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

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 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