2

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

enter image description here

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;
10
  • The keys in your object all start with lower case letter, e.g. code, but you access them in your code with capital letter, e.g. Code? Commented Mar 10, 2022 at 10:27
  • that doesnt seem the problem. I have change it and the result is same Commented Mar 10, 2022 at 10:33
  • Could you log response.data and post the result? Commented Mar 10, 2022 at 10:37
  • @DavidScholz I have update the question and post the response.data. Commented Mar 10, 2022 at 10:49
  • Ooh, it looks like you have an array in an array and in that array is your object. Does response.data[0][0]?.code work? Commented Mar 10, 2022 at 11:04

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.