I got a strange push object in here
[
[
{
"buyerVolume": "617,742",
"buyerVolumePercent": "71.42857",
"chart": "c1",
"code": 200,
"company": "Bank Central Asia Tbk.",
"date": "3/10/2022",
"id": 1,
"link": "https://blablabla.id",
"ma200": "7,011",
"ma60": "7,674",
"message": "Pull Data Successfuly",
"pivotHighLow": undefined,
"resistance": "8,225",
"sellerVolume": "247,097",
"sellerVolumePercent": undefined,
"status": "Success",
"support": "7,650",
"takingProfit": "8,350",
"ticker": "BBCA",
"trailStop": "7,750",
"value": "683,223,600,000",
},
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
],
]
I got so many undefined
This is how I push the data :
let modelData = new ApiChartingDataModel();
let data = [];
modelData = new ApiChartingDataModel ({
id: 1,
code: response.data.Code,
status: response.data.Status,
message: response.data.Message,
link: response.data.Data.Link,
chart: response.data.Data.Chart,
company: response.data.Data.Company,
date: response.data.Data.Date,
ticker: response.data.Data.Ticker,
value: response.data.Data.Value,
sellerVolume: response.data.Data.SellerVolume,
sellerVolumePercent: response.data.Data.SellerVolumePercent,
buyerVolume: response.data.Data.BuyerVolume,
buyerVolumePercent: response.data.Data.BuyerVolumePercent,
ma60: response.data.Data.MA60,
ma200: response.data.Data.MA200,
resistance: response.data.Data.Resistance,
support: response.data.Data.Support,
pivotHighLow: response.data.Data.PivotHighLow,
takingProfit: response.data.Data.TakingProfit,
trailStop: response.data.Data.TrailStop
});
data.push(Object.values(modelData).flat());
when console.log(data), I got so many undefined. I just push one model structure and didn't do looping, but I got undefined.
Why it is appear right that ? and how to solve it ?
Edit
Second Edit
This is my Model Data
class ApiChartingDataModel {
constructor(id, code, status, message, link, chart, company, date, ticker, value, sellerVolume, sellerVolumePercent, buyerVolume, buyerVolumePercent, ma60, ma200, resistence, support, pivotHightLow, takingProfit, trailStop) {
this.id = id;
this.code = code;
this.status = status;
this.message = message;
this.link = link;
this.chart = chart;
this.company = company;
this.date = date;
this.ticker = ticker;
this.value = value;
this.sellerVolume = sellerVolume;
this.sellerVolumePercent = sellerVolumePercent;
this.buyerVolume = buyerVolume;
this.buyerVolumePercent = buyerVolumePercent;
this.ma60 = ma60;
this.ma200 = ma200;
this.resistence = resistence;
this.support = support;
this.pivotHightLow = pivotHightLow;
this.takingProfit = takingProfit;
this.trailStop = trailStop;
}
}
export default ApiChartingDataModel;
CodePudding user response:
You receive an array which contains an array and inside of it, there is your object, thus the following code allows you to access it.
// accessing the object with no guards against empty arrays or undefined
const obj = response.data[0][0]
console.log(obj.code)
It now depends on your data. If you know that there will be always one array inside an array and all objects will be inside of it (or there will always be a single object), then we could make this easier as follows.
Multiple objects inside nested array
const dummyData = [
[
{
"buyerVolume": "617,742",
"buyerVolumePercent": "71.42857",
"chart": "c1",
"code": 200,
"company": "Bank Central Asia Tbk.",
"date": "3/10/2022",
"id": 1,
"link": "https://blablabla.id",
"ma200": "7,011",
"ma60": "7,674",
"message": "Pull Data Successfuly",
"pivotHighLow": undefined,
"resistance": "8,225",
"sellerVolume": "247,097",
"sellerVolumePercent": undefined,
"status": "Success",
"support": "7,650",
"takingProfit": "8,350",
"ticker": "BBCA",
"trailStop": "7,750",
"value": "683,223,600,000",
},
{
"buyerVolume": "617,742",
"buyerVolumePercent": "71.42857",
"chart": "c1",
"code": 200,
"company": "Bank Central Asia Tbk.",
"date": "3/10/2022",
"id": 1,
"link": "https://blablabla.id",
"ma200": "7,011",
"ma60": "7,674",
"message": "Pull Data Successfuly",
"pivotHighLow": undefined,
"resistance": "8,225",
"sellerVolume": "247,097",
"sellerVolumePercent": undefined,
"status": "Success",
"support": "7,650",
"takingProfit": "8,350",
"ticker": "BBCA",
"trailStop": "7,750",
"value": "683,223,600,000",
},
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
],
]
const data = dummyData.flat().filter(a => a)
console.log(data)
The above filters all undefined
objects inside the nested array and flattens the parent array resulting in an array of objects.
You know that you will always have one object
const dummyData = [
[
{
"buyerVolume": "617,742",
"buyerVolumePercent": "71.42857",
"chart": "c1",
"code": 200,
"company": "Bank Central Asia Tbk.",
"date": "3/10/2022",
"id": 1,
"link": "https://blablabla.id",
"ma200": "7,011",
"ma60": "7,674",
"message": "Pull Data Successfuly",
"pivotHighLow": undefined,
"resistance": "8,225",
"sellerVolume": "247,097",
"sellerVolumePercent": undefined,
"status": "Success",
"support": "7,650",
"takingProfit": "8,350",
"ticker": "BBCA",
"trailStop": "7,750",
"value": "683,223,600,000",
},
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
],
]
const data = dummyData.flat().filter(a => a).pop()
console.log(data)