繁体   English   中英

我想将 object 推送到一个新数组中,其中 free 大于 1

[英]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,} 放入一个数组并得到这样的结果 [{"free": 1, "used": 0,"total": 0, "usd_price": 0},{ “免费”:1,“使用”:0,“总计”: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
    }

}
}

您在这里要做的是利用 Javascript 的filter方法来获取一个通过给定条件的子数组,在您的情况下确保free的值大于 1。但是 filter 方法只能应用于一个array ,但您提供的数据是object的形式。

因此,在应用filter方法之前,让我们使用Object.values()将此 object 转换为数组。

Object.values(json.data)

获得数组格式的数据后,我们现在可以应用 filter 方法从原始数组中获取相关子集(同样,在您的情况下,它是 free 值大于 1 的元素)

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

它现在应该可以工作了! 这是下面的完整工作代码

 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)

尝试这个:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM