0

I have the following array, I want to loop through it and push the key values to another declared array. Then, loop through the array and push field and values of a key to another array.

timeframe = []; timeframeValueKeyUSD = []; timeframeValueKeyCAD = []; array = [ { "2012-05-01":{ "USD": 1.322891, "AUD": 1.278047, "CAD": 1.302303 }, "2012-05-02": { "USD": 1.315066, "AUD": 1.274202, "CAD": 1.299083 }, "2012-05-03": { "USD": 1.314491, "AUD": 1.280135, "CAD": 1.296868 } } ] 

I want to get the arrays as this:

timeframe = ['2012-05-01', '2012-05-02', '2012-05-03']; timeframeValueKeyUSD = [1.315066, 1.315066, 1.314491]; timeframeValueKeyCAD = [1.302303, 1.299083, 1.296868]; 
1

4 Answers 4

2

You could get the dates first and then map the values according to the dates.

function getKeys(o) { return Object.keys(o); } function getValueBy(object, groups, key) { return groups.map(k => object[k][key]); } var array = [{ "2012-05-01":{ USD: 1.322891, AUD: 1.278047, CAD: 1.302303 }, "2012-05-02": { USD: 1.315066, AUD: 1.274202, CAD: 1.299083 }, "2012-05-03": { USD: 1.314491, AUD: 1.280135, CAD: 1.296868 } }], timeframe = getKeys(array[0]); timeframeValueKeyUSD = getValueBy(array[0], timeframe, 'USD'), timeframeValueKeyCAD = getValueBy(array[0], timeframe, 'CAD'); console.log(timeframe); console.log(timeframeValueKeyUSD); console.log(timeframeValueKeyCAD);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

1

Use a simple forEach() loop to get the desired output.

timeframe = []; var timeframeValueKeyUSD = []; var timeframeValueKeyCAD = []; var array = [ { "2012-05-01":{ "USD": 1.322891, "AUD": 1.278047, "CAD": 1.302303 }, "2012-05-02": { "USD": 1.315066, "AUD": 1.274202, "CAD": 1.299083 }, "2012-05-03": { "USD": 1.314491, "AUD": 1.280135, "CAD": 1.296868 } } ] array.forEach((obj)=>{ var keys = Object.keys(obj); timeframe.push(...keys); keys.forEach((key)=>{ timeframeValueKeyUSD.push(obj[key].USD); timeframeValueKeyCAD.push(obj[key].CAD); }); }); console.log(timeframe); console.log(timeframeValueKeyUSD); console.log(timeframeValueKeyCAD);

Comments

1

You can use for loop to traverse :

var timeframe = []; var timeframeValueKeyUSD = []; var timeframeValueKeyCAD = []; var array = [{"2012-05-01":{"USD":1.322891,"AUD":1.278047,"CAD":1.302303},"2012-05-02":{"USD":1.315066,"AUD":1.274202,"CAD":1.299083},"2012-05-03":{"USD":1.314491,"AUD":1.280135,"CAD":1.296868}}]; for (var t in array[0]) { timeframe.push(t); timeframeValueKeyUSD.push(array[0][t].USD); timeframeValueKeyCAD.push(array[0][t].CAD); } console.log(timeframe, timeframeValueKeyUSD, timeframeValueKeyCAD);

Comments

0

A shorter ES6 declarative alternative:

const dataObj = array[0]; const { timeframe, timeframeValueKeyUSD, timeframeValueKeyCAD } = Object.keys(dataObj).reduce((all, timeframe) => { const { USD, CAD } = dataObj[timeframe]; all.timeframe.push(timeframe); all.timeframeValueKeyUSD.push(USD); all.timeframeValueKeyCAD.push(CAD); return all; }, {timeframe: [], timeframeValueKeyUSD: [], timeframeValueKeyCAD: []}); console.log(timeframe); console.log(timeframeValueKeyUSD); console.log(timeframeValueKeyCAD); 

If you want to make it even more dynamic (to cover all possible currencies):

const dataObj = array[0]; const result = Object.keys(dataObj).reduce((all, timeframe) => { const currencies = Object.keys(dataObj[timeframe]); all.timeframe.push(timeframe); currencies.forEach(currency => { if (!all.hasOwnProperty(`timeframeValueKey${currency}`)) { all[`timeframeValueKey${currency}`] = []; } all[`timeframeValueKey${currency}`].push(dataObj[timeframe][currency]); }); return all; }, {timeframe: [] }); console.log(result); timeframe:(3) ["2012-05-01", "2012-05-02", "2012-05-03"] timeframeValueKeyAUD:(3) [1.278047, 1.274202, 1.280135] timeframeValueKeyCAD:(3) [1.302303, 1.299083, 1.296868] timeframeValueKeyUSD:(3) [1.322891, 1.315066, 1.314491] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.