Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!