I have an object which consists of newSales and oldSales and I am looping over them to get the key value pairs. I have been able to console log them but now I am stuck. How do I push those key value pairs in my array? This is a sample codepen
const obj = {
newSales: 1,
oldSales: 0,
anotherProp: 'something',
etc: 'etc',
values: 'numbers'
}
function myDataSet() {
let dataSet = []
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(`${key} : ${obj[key]}`)
dataSet.push({
name: ['newSales', 'oldSales'],
data: [1, 0]
})
}
}
return dataSet
}
myDataSet();
The new Name in the object should be the name i.e. newSales and oldSales and data inside should be an array with the values 1 and 0. I appreciate all the help.
Expected output:
[ { name: ['newSales', 'oldSales'], data:[ 1,0 ] } ]
[ { name: "newSales", data: [1, 0] }, { name: "oldSales", data: [1, 0] }]. That doesn't make sense[ { name: ['newSales', 'oldSales'], data:[ 1,0 ] }. Thank you for pointing that out. I Should have been more clear.