0

I get the data from Backend which is given below. and have to manipulate the data as per the requirement. how to convert the following JSON Array format as per the requirement in ReactJS. the Json Data given below

[
    {
      "date": "2020-12-10",
      "airline": "Air asia",
      "totalprice": 4180,
      "departuretime": "20:25"
    },
    {
      "date": "2020-12-10",
      "airline": "Air asia",
      "totalprice": 4180,
      "departuretime": "11:25"
    },
    {
      "date": "2020-12-10",
      "airline": "Air asia",
      "totalprice": 4180,
      "departuretime": "22:45"
    },
    {
      "date": "2020-12-10",
      "airline": "Air india",
      "totalprice": 4180,
      "departuretime": "06:10",
    },
    {
      "date": "2020-12-10",
      "airline": "Air india",
      "totalprice": 4180,
      "departuretime": "13:50",
    },
    {
      "date": "2020-12-10",
      "airline": "Air india",
      "totalprice": 4180,
      "departuretime": "07:05",
    }
]

convert the above data as per the following data

[
  {
    "airline": "Air asia",
    "data": [
      {
        "date": "2020-12-10",
        "totalprice": 5180,
        "departuretime": "20:25"
      },
      {
        "date": "2020-12-10",
        "totalprice": 4180,
        "departuretime": "11:25"
      },
      {
        "date": "2020-12-10",
        "totalprice": 4180,
        "departuretime": "22:45"
      }
    ]
  },
  {
    "airline": "Air india",
    "data": [
      {
        "date": "2020-12-10",
        "totalprice": 4180,
        "departuretime": "06:10"
      },
      {
        "date": "2020-12-10",
        "totalprice": 4180,
        "departuretime": "13:50"
      },
      {
        "date": "2020-12-10",
        "totalprice": 4180,
        "departuretime": "07:05"
      }
    ]
  }
]

How to convert the by the Javascript method ?

1
  • What have you tried? Have you tried reading array methods? Commented Dec 14, 2020 at 6:53

4 Answers 4

1

let data = [
{
  "date": "2020-12-10",
  "airline": "Air asia",
  "totalprice": 4180,
  "departuretime": "20:25"
},
{
  "date": "2020-12-10",
  "airline": "Air asia",
  "totalprice": 4180,
  "departuretime": "11:25"
},
{
  "date": "2020-12-10",
  "airline": "Air asia",
  "totalprice": 4180,
  "departuretime": "22:45"
},
{
  "date": "2020-12-10",
  "airline": "Air india",
  "totalprice": 4180,
  "departuretime": "06:10",
},
{
  "date": "2020-12-10",
  "airline": "Air india",
  "totalprice": 4180,
  "departuretime": "13:50",
},
{
  "date": "2020-12-10",
  "airline": "Air india",
  "totalprice": 4180,
  "departuretime": "07:05",
}
]
// use can use this object as it is too.
let sol = {}
data.forEach(d => {
  if(sol[d.airline]){
sol[d.airline].data.push({"date": d.date, "totalprice": d.totalprice, "departuretime": d.departuretime})
  }else{
sol[d.airline] = {"data" : [{"date": d.date, "totalprice": d.totalprice, "departuretime": d.departuretime}]}
  }
})

let d = Object.keys(sol).map(key => {
  return {"airline": key, "data": sol[key]["data"]}
})

console.log(d)

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

Comments

0

You can use the reduce function to solve this. See below:

const airlinesReducer = (accumulator, current) => {
  // Breakdown the entry into name and other fields
  let { airline, ...airlineFields} = current;

  // Look for a previously indexed entry
  let airlineData = accumulator.indexed[airline]
  
  // If the airline hasn't been indexed before, add it to the index map
  if (!airlineData) {
    // Create a reference for the new array to be filled
    airlineData = accumulator.indexed[airline] = []
    accumulator.result.push({
      airline: current.airline,
      data: airlineData
    })
  } 
  
  // Push this entry to the results array
  airlineData.push(airlineFields)
  return accumulator
}

let result = array.reduce(airlinesReducer, { indexed: {}, result: [] }).result

2 Comments

Why is it overkill?
Coincidentally there's a tweet... twitter.com/dan_abramov/status/1338253118199508992
0

const input = [{"date":"2020-12-10","airline":"Air asia","totalprice":4180,"departuretime":"20:25"},{"date":"2020-12-10","airline":"Air asia","totalprice":4180,"departuretime":"11:25"},{"date":"2020-12-10","airline":"Air asia","totalprice":4180,"departuretime":"22:45"},{"date":"2020-12-10","airline":"Air india","totalprice":4180,"departuretime":"06:10"},{"date":"2020-12-10","airline":"Air india","totalprice":4180,"departuretime":"13:50"},{"date":"2020-12-10","airline":"Air india","totalprice":4180,"departuretime":"07:05"}];

// Group data...
const groups = {};
input.forEach(({
  airline,
  ...rest
}) => (groups[airline] = groups[airline] || []).push(rest));

// ... and transform back to array.
const output = Object.entries(groups).map(([airline, data]) => ({
  airline,
  data
}));
console.log(output);

Comments

0

Here is another solution with some ES6 code

const arr = [
  {
    date: '2020-12-10',
    airline: 'Air asia',
    totalprice: 4180,
    departuretime: '20:25',
  },
  {
    date: '2020-12-10',
    airline: 'Air asia',
    totalprice: 4180,
    departuretime: '11:25',
  },
  {
    date: '2020-12-10',
    airline: 'Air asia',
    totalprice: 4180,
    departuretime: '22:45',
  },
  {
    date: '2020-12-10',
    airline: 'Air india',
    totalprice: 4180,
    departuretime: '06:10',
  },
  {
    date: '2020-12-10',
    airline: 'Air india',
    totalprice: 4180,
    departuretime: '13:50',
  },
  {
    date: '2020-12-10',
    airline: 'Air india',
    totalprice: 4180,
    departuretime: '07:05',
  },
];

//Using a Set datastructure to capture individual airline names
let uniqueAirlinesSet = new Set();
for (let el of arr) {
  uniqueAirlinesSet.add(el.airline);
}

// Converted set to array of unique elements like this["Air Asia","Air India"]
const uniqueAirlinesArr = [...uniqueAirlinesSet];

const newFormattedArray = uniqueAirlinesArr.reduce((acc, el) => {
  const obj = {
    airline: el,
    data: [],
  };
  for (let { airline, date, departuretime, totalprice } of arr) {
    if (el === airline) {
      obj.data.push({ date, departuretime, totalprice });
    }
  }
  acc.push(obj);
  return acc;
}, []);

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.