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
}
}
}
CodePudding user response:
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)
CodePudding user response:
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)
