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..

4 Answers 4

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;
}, {});
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

why downvote, what is wrong with this solution, please provide the reason also :(
I agree. There is a guy who put -1 to each answer (including mine). This is unfair, so I give you an upvote to compensate.
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);

Comments

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) {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.