I have the following object:
let obj = {
"productData": [{
"id": 0,
"daily_netProfit": 1.35208688924,
"daily_grossProfit": 1.35448688924,
"daily_costs": 0.0024,
"created_at": "2018-06-18"
},
{
"id": 1,
"daily_netProfit": 1.35208688924,
"daily_grossProfit": 1.35448688924,
"daily_costs": 0.0024,
"created_at": "2018-06-19"
}
]
}
let dataSet = obj.productData.map((item, i) => [
item.daily_netProfit,
item.daily_grossProfit,
item.daily_costs,
item.created_at,
])
console.log(dataSet)
As you can see the above map function constructs an array within an array. However, my final result array with objects should look like the following:
[{
daily_netProfit: 1.35208688924,
daily_grossProfit: 1.35448688924,
daily_costs: 0.0024,
day: "2018-06-18"
},
{
daily_netProfit: 1.35208688924,
daily_grossProfit: 1.35448688924,
daily_costs: 0.0024,
day: "2018-06-19"
}
],
Is there a map function to construct the above array-object?
I appreciate your replies!
Krupesh Kotecha
2,4123 gold badges23 silver badges40 bronze badges
asked Jun 18, 2018 at 11:20
Carol.Kar
5,29138 gold badges150 silver badges305 bronze badges
-
Why don't you just create an object instead of an array as you need to?Yury Tarabanko– Yury Tarabanko2018年06月18日 11:22:40 +00:00Commented Jun 18, 2018 at 11:22
2 Answers 2
You could map an object.
var obj = { productData: [{ id: 0, daily_netProfit: 1.35208688924, daily_grossProfit: 1.35448688924, daily_costs: 0.0024, created_at: "2018-06-18" }, { id: 1, daily_netProfit: 1.35208688924, daily_grossProfit: 1.35448688924, daily_costs: 0.0024, created_at: "2018-06-19" }] },
dataSet = obj.productData.map(
({ daily_netProfit, daily_grossProfit, daily_costs, created_at: day }) =>
({ daily_netProfit, daily_grossProfit, daily_costs, day }))
console.log(dataSet)
.as-console-wrapper { max-height: 100% !important; top: 0; }
With rest syntax for objects (for newest JS) even shorter
var obj = { productData: [{ id: 0, daily_netProfit: 1.35208688924, daily_grossProfit: 1.35448688924, daily_costs: 0.0024, created_at: "2018-06-18" }, { id: 1, daily_netProfit: 1.35208688924, daily_grossProfit: 1.35448688924, daily_costs: 0.0024, created_at: "2018-06-19" }] },
dataSet = obj.productData.map(({ id, created_at: day, ...rest }) => ({ ...rest, day }));
console.log(dataSet);
.as-console-wrapper { max-height: 100% !important; top: 0; }
answered Jun 18, 2018 at 11:24
Nina Scholz
388k26 gold badges367 silver badges417 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
You're creating arrays rather than objects. Create objects instead. You can use parameter destructuring to pick out hte parts you want, then use an object initializer to create the new object:
let obj = {
"productData": [{
"id": 0,
"daily_netProfit": 1.35208688924,
"daily_grossProfit": 1.35448688924,
"daily_costs": 0.0024,
"created_at": "2018-06-18"
},
{
"id": 1,
"daily_netProfit": 1.35208688924,
"daily_grossProfit": 1.35448688924,
"daily_costs": 0.0024,
"created_at": "2018-06-19"
}
]
};
let dataSet = obj.productData.map(
({
daily_netProfit,
daily_grossProfit,
daily_costs,
created_at
}) => ({
daily_netProfit,
daily_grossProfit,
daily_costs,
created_at
})
);
console.log(dataSet)
.as-console-wrapper {
max-height: 100% !important;
}
answered Jun 18, 2018 at 11:26
T.J. Crowder
1.1m201 gold badges2k silver badges2k bronze badges
Comments
lang-js