0

Give the title:

row.push({"ticker" : PX_Hist[j]['ticker']});

Calcluate data with different timeframe parameters

   const timeframes =  [5,10,90,120,250,500];

for (let t = 0; t <= timeframes.length - 1; t++){
            row.push({"move" : ((PX_Hist[j]['px'][PX_Hist[j]['px'].length-1]["adjusted_close"] / PX_Hist[j]['px'][PX_Hist[j]['px'].length-timeframes[t]]["adjusted_close"] -1))});
        }

I am creating the following output with this code.

[
  [
    {
      "ticker": "JPM"
    },
    {          "move": 0.01405944118170499        },
    {          "move": 0.0337445573294628        },
    {          "move": 0.1692882281117576        },
    {          "move": 0.07636499188035195        },
    {          "move": 0.8151371865267423        },
    {          "move": 0.4537049320855997        }
  ],
  [
    {
      "ticker": "C"
    },
    {          "move": -0.01295986622073586        },
    {          "move": 0.002689694224235595        },
    {          "move": 0.05544868117343582        },
    {          "move": -0.0457495911125243        },
    {          "move": 0.7837535634777528        },
    {          "move": 0.05665004788714745        }
  ],
  [
    {
      "ticker": "C"
    },
    {          "move": -0.01295986622073586        },
    {          "move": 0.002689694224235595        },
    {          "move": 0.05544868117343582        },
    {          "move": -0.0457495911125243        },
    {          "move": 0.7837535634777528        },
    {          "move": 0.05665004788714745        }
  ],
  
]

I need to transpose the above array to something that I can easily bind to a table like below:

    [{"ticker": "JPM", "5": 0.01405944118170499,"10": 0.0337445573294628,"90": 
    0.1692882281117576,"120": 0.07636499188035195,"250": 0.8151371865267423,"500": 
    0.4537049320855997}]

Any words of advice about how to do this in an elegant way?

6
  • What is "elegant" for you? Commented Oct 26, 2021 at 18:27
  • Preferable a functional statement instead of the first for loop that could push the data into the appropriate columns. Commented Oct 26, 2021 at 18:29
  • Or maybe the initial JSON should be constructed differently before pushing in to the array. Commented Oct 26, 2021 at 18:33
  • Yeah, it's what I'd suggest. But I think you meant an object, JSON is not the same. Commented Oct 26, 2021 at 18:34
  • Do I concatenate text or is there a way to loop while creating the json? Commented Oct 26, 2021 at 18:35

1 Answer 1

1

You can so something like this, using map and for loop. But honestly I find this unnecessary. You could just do this from the get go in your loop. instead of pushing to row, you could try:

row[0][timeframes[t]] = "that long thing you have there"

const arr = [
  [{
      "ticker": "JPM"
    },
    {
      "move": 0.01405944118170499
    },
    {
      "move": 0.0337445573294628
    },
    {
      "move": 0.1692882281117576
    },
    {
      "move": 0.07636499188035195
    },
    {
      "move": 0.8151371865267423
    },
    {
      "move": 0.4537049320855997
    }
  ],
  [{
      "ticker": "C"
    },
    {
      "move": -0.01295986622073586
    },
    {
      "move": 0.002689694224235595
    },
    {
      "move": 0.05544868117343582
    },
    {
      "move": -0.0457495911125243
    },
    {
      "move": 0.7837535634777528
    },
    {
      "move": 0.05665004788714745
    }
  ],
  [{
      "ticker": "C"
    },
    {
      "move": -0.01295986622073586
    },
    {
      "move": 0.002689694224235595
    },
    {
      "move": 0.05544868117343582
    },
    {
      "move": -0.0457495911125243
    },
    {
      "move": 0.7837535634777528
    },
    {
      "move": 0.05665004788714745
    }
  ],

]

const flatObj = (arr) => {
  const flatObject = {};
  const keys = ["ticker", "5", "10", "90", "120", "250", "500"]
  for (let i = 0; i < arr.length; i++) {
    for (const property in arr[i]) {
      flatObject[keys[i]] = arr[i][property];
    }
  };
  return flatObject;
}

const result = arr.map(ar => flatObj(ar))

console.log(result)

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.