Уменьшить JSON данных - PullRequest
       4

Уменьшить JSON данных

0 голосов
/ 13 апреля 2020

У меня JSON данные:

var data = [
    { "Date": "\/Date(1583778600000)\/", "Rate": 1310, "Product": "Prod1" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1320, "Product": "Prod2" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1330, "Product": "Prod3" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1340, "Product": "Prod4" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1350, "Product": "Prod5" },
    { "Date": "\/Date(1584037800000)\/", "Rate": 1360, "Product": "Prod6" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1370, "Product": "Prod7" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1380, "Product": "Prod8" }
];

Я хочу преобразовать это в нечто вроде этого:

var result = [
    { "Date": "2020-03-10", "Rate": { 1310: "Prod1", 1340: "Prod4", 1350: "Prod5" } },
    { "Date": "2020-03-11", "Rate": { 1320: "Prod2", 1370: "Prod7" } },
    { "Date": "2020-03-12", "Rate": { 1330: "Prod3", 1380: "Prod8" } },
    { "Date": "2020-03-13", "Rate": { 1360: "Prod6" } }
];

Спасибо

Ответы [ 3 ]

1 голос
/ 13 апреля 2020
var data = [
    { "Date": "\/Date(1583778600000)\/", "Rate": 1310, "Product": "Prod1" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1320, "Product": "Prod2" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1330, "Product": "Prod3" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1340, "Product": "Prod4" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1350, "Product": "Prod5" },
    { "Date": "\/Date(1584037800000)\/", "Rate": 1360, "Product": "Prod6" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1370, "Product": "Prod7" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1380, "Product": "Prod8" }
];

var o = data.reduce((a, b) => {
    b.Date = moment(b.Date).format('YYYY-MM-DD');
    a[b.Date] = a[b.Date] || [];
    a[b.Date].push({ [b.Rate]: b.Product });
    return a;
}, {});

var result = Object.keys(o).map(function (k) {
    return { Date: k, Rate: Object.assign.apply({}, o[k]) };
});

var data = [
    { "Date": "\/Date(1583778600000)\/", "Rate": 1310, "Product": "Prod1" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1320, "Product": "Prod2" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1330, "Product": "Prod3" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1340, "Product": "Prod4" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1350, "Product": "Prod5" },
    { "Date": "\/Date(1584037800000)\/", "Rate": 1360, "Product": "Prod6" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1370, "Product": "Prod7" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1380, "Product": "Prod8" }
];

var o = data.reduce((a, b) => {
    b.Date = moment(b.Date).format('YYYY-MM-DD');
    a[b.Date] = a[b.Date] || [];
    a[b.Date].push({ [b.Rate]: b.Product });
    return a;
}, {});

var result = Object.keys(o).map(function (k) {
    return { Date: k, Rate: Object.assign.apply({}, o[k]) };
});

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
0 голосов
/ 13 апреля 2020

Вы можете сделать что-то вроде следующего с vanilla JavaScript:

var data = [
  { Date: "/Date(1583778600000)/", Rate: 1310, Product: "Prod1" },
  { Date: "/Date(1583865000000)/", Rate: 1320, Product: "Prod2" },
  { Date: "/Date(1583951400000)/", Rate: 1330, Product: "Prod3" },
  { Date: "/Date(1583778600000)/", Rate: 1340, Product: "Prod4" },
  { Date: "/Date(1583778600000)/", Rate: 1350, Product: "Prod5" },
  { Date: "/Date(1584037800000)/", Rate: 1360, Product: "Prod6" },
  { Date: "/Date(1583865000000)/", Rate: 1370, Product: "Prod7" },
  { Date: "/Date(1583951400000)/", Rate: 1380, Product: "Prod8" },
];

let result = data.reduce((p, c) => {
  let date = new Date(parseInt(c.Date.match(/(\d+)/)[0]));
  let formattedDate = date.toISOString().slice(0, 10);
  let existing = p.find((o) => o.Date === formattedDate);
  existing
    ? (existing.Rate = { ...existing.Rate, [c.Rate]: c.Product })
    : p.push({ Date: formattedDate, Rate: { [c.Rate]: c.Product } });
  return p;
}, []);

console.log(result)
0 голосов
/ 13 апреля 2020

Надеюсь, это поможет

let data = [
    { "Date": "\/Date(1583778600000)\/", "Rate": 1310, "Product": "Prod1" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1320, "Product": "Prod2" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1330, "Product": "Prod3" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1340, "Product": "Prod4" },
    { "Date": "\/Date(1583778600000)\/", "Rate": 1350, "Product": "Prod5" },
    { "Date": "\/Date(1584037800000)\/", "Rate": 1360, "Product": "Prod6" },
    { "Date": "\/Date(1583865000000)\/", "Rate": 1370, "Product": "Prod7" },
    { "Date": "\/Date(1583951400000)\/", "Rate": 1380, "Product": "Prod8" }
];

let list = [];

data = data.map(item => ({
  ...item,
  Date: Number(item.Date.split(/[()]/)[1])
}));

list = data.map(item => item.Date);

list = [ ...new Set(list) ]

list = list.sort((a, b) =>  a - b);

list = list.map(date => {
  const filteredDate = data.filter(item => item.Date === date);
  const rate = filteredDate.reduce((acc, cur) => ({ ...acc, [cur.Rate]: cur.Product }), {});

  const d = new Date(date);
  let month = '' + (d.getMonth() + 1);
  let day = '' + d.getDate();
  const year = d.getFullYear();

  if (month.length < 2) month = '0' + month;
  if (day.length < 2) day = '0' + day;

  return {
    Date: [year, month, day].join('-'),
    Rate: rate
  }
});

console.log(list);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...