2

I have an array of objects like this one:

[
    {
        "dow": 0,
        "min_price": 2000,
        "max_price": 4000,
        "is_enable": true
    },
    {
        "dow": 1,
        "min_price": 500,
        "max_price": 3000,
        "is_enable": true
    },
    {
        "dow": 2,
        "min_price": 500,
        "max_price": 3000,
        "is_enable": true
    },
    {
        "dow": 3,
        "min_price": 1000,
        "max_price": 3000,
        "is_enable": true
    }
]

How can I turn it into something like this one? The order doesn't matter.

{
    advancePrice_0_max: 4000,
    advancePrice_0_min: 2000,
    advancePrice_1_max: 3000,
    advancePrice_1_min: 500,
    advancePrice_2_max: 3000,
    advancePrice_2_min: 500,
    advancePrice_3_max: 3000,
    advancePrice_3_min: 500
    ..
    ..
}

I've tried to begin with this, but even below code has snytax error.

let temp = {};
if (!isEmpty.custom_pricing) {
  temp = custom_pricing.map(obj => {
    `advancePrice_${obj.dow}_min`: obj.min_price,
    `advancePrice_${obj.dow}_max`: obj.max_price
  })
  console.log(temp);
}

But I'm stuck and don't know how to continue..

lunochkin
  • 684
  • 4
  • 17
Giala Jefferson
  • 1,809
  • 8
  • 20
  • 29

4 Answers4

1

You want to reduce the array to a single object, so:

var result = data.reduce(function (acc,el,i) {
  acc['advancedprice_' + i + '_max'] = el.max_price;
  acc['advancedprice_' + i + '_min'] = el.min_price;
  return acc;
}, {});
James
  • 20,957
  • 5
  • 26
  • 41
0

You have two choices:

  • Write your own solution
  • Use someone else's solution

I advise you to use someone else's solution, because you don't have to reinvent the wheel. This answer is exhaustive with examples: Fastest way to flatten / un-flatten nested JSON objects, and there is even an online example here: http://fiddle.jshell.net/blowsie/S2hsS/show/light/

See the code snippets of:

JSON.flatten = function (data) { ...

and:

JSON.unflatten = function (data) {
clairestreb
  • 1,243
  • 12
  • 24
0

Use forEach because you want the result as an object not in array format, if you use map then temp will be an array with returned data.

use this:

let temp = {};
if (!isEmpty.custom_pricing) {
   custom_pricing.forEach(obj => {
      temp[`advancePrice_${obj.dow}_min`] = obj.min_price;
      temp[`advancePrice_${obj.dow}_max`] = obj.max_price;
  })
  console.log(temp);
}

Check the working snippet:

let custom_pricing = [{
        "dow": 0,
        "min_price": 2000,
        "max_price": 4000,
        "is_enable": true
    },
    {
        "dow": 1,
        "min_price": 500,
        "max_price": 3000,
        "is_enable": true
    },
    {
        "dow": 2,
        "min_price": 500,
        "max_price": 3000,
        "is_enable": true
    },
    {
        "dow": 3,
        "min_price": 1000,
        "max_price": 3000,
        "is_enable": true
    }
]

let temp = {};
custom_pricing.forEach(obj => {
   temp[`advancePrice_${obj.dow}_min`] = obj.min_price;
   temp[`advancePrice_${obj.dow}_max`] = obj.max_price;
})
console.log(temp);
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
0

You could use the good old for loop...

let arr = [
  {
    "dow": 0,
    "min_price": 2000,
    "max_price": 4000,
    "is_enable": true
  },
  {
    "dow": 1,
    "min_price": 500,
    "max_price": 3000,
    "is_enable": true
  },
  {
    "dow": 2,
    "min_price": 500,
    "max_price": 3000,
    "is_enable": true
  },
  {
    "dow": 3,
    "min_price": 1000,
    "max_price": 3000,
    "is_enable": true
  }
];

let res = {};

for (let i = 0; i < arr.length; i++) {
  res[`advancePrice_${i}_max`] = arr[i].max_price;
  res[`advancePrice_${i}_min`] = arr[i].min_price;
}

console.log(res);
Badacadabra
  • 8,043
  • 7
  • 28
  • 49