0

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!

1
  • Why don't you just create an object instead of an array as you need to? Commented Jun 18, 2018 at 11:22

2 Answers 2

3

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; }

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

Comments

1

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;
}

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.