JSON和Javascript(ES6)的复杂过程

时间:2018-07-06 00:57:35

标签: javascript json reactjs

好的,我必须使用此JSON:

https://gist.github.com/pedroapfilho/c1673be24dfdb36133149b4edfc8c2bd

和:

1-按字母顺序列出类别(并过滤以显示唯一值)

2-以订阅价格的总和作为参数,按升序排列此数组

非常感谢!

1 个答案:

答案 0 :(得分:0)

尝试一下。

let json = [{
    "id": "9b565b11-7311-5b5e-a699-97873dffb361",
    "name": "Voice Report",
    "description": "Calls reporting and analytics of your calls.",
    "categories": ["Voice Analytics", "Reporting", "Optimization"],
    "subscriptions": [{
        "name": "Trial",
        "price": 0
      },
      {
        "name": "Professional",
        "price": 3500
      }
    ]
  },
  {
    "id": "470fedc5-489e-5acb-a200-c85adaa18356",
    "name": "Power Dialer",
    "description": "Auto dialer that will help increase your connect rates and talk time.",
    "categories": ["Dialer"],
    "subscriptions": [{
        "name": "Trial",
        "price": 0
      },
      {
        "name": "Professional",
        "price": 4500
      },
      {
        "name": "Premium",
        "price": 6000
      }
    ]
  },
  {
    "id": "52714d80-e3c4-5593-b9a3-e2ff484be372",
    "name": "Smart Text",
    "description": "Use SMS to help you communicate with your customers.",
    "categories": ["Channels"],
    "subscriptions": [{
      "name": "Trial",
      "price": 0
    }]
  },
  {
    "id": "8d68c357-59e6-505a-b0e1-4953196b14df",
    "name": "Customer Chat",
    "description": "Improve your call center with live chat support.",
    "categories": ["Channels"],
    "subscriptions": [{
      "name": "Trial",
      "price": 0
    }]
  },
  {
    "id": "dd024ed5-efae-5785-addc-09e592066e5c",
    "name": "Report Plus",
    "description": "Advanced reporting with custom dashboards.",
    "categories": ["Reporting"],
    "subscriptions": [{
        "name": "Starter",
        "price": 2000
      },
      {
        "name": "Plus",
        "price": 4500
      }
    ]
  },
  {
    "id": "f820ad5d-32d0-5bb7-aed4-cbc74bcf0b47",
    "name": "Screen Share",
    "description": "Enable screen sharing between your agents and customers.",
    "categories": ["Productivity"],
    "subscriptions": [{
      "name": "Professional",
      "price": 6000
    }]
  },
  {
    "id": "7f89f001-9d7d-52f1-82cb-8f44eb1e4680",
    "name": "Video Contacts",
    "description": "Communicate with your customers and agents using video calls.",
    "categories": ["Productivity"],
    "subscriptions": [{
        "name": "Trial",
        "price": 0
      },
      {
        "name": "Professional",
        "price": 2500
      }
    ]
  },
  {
    "id": "32be8940-aeb6-5325-ae63-6497772f362a",
    "name": "Agent Monitor",
    "description": "More tools to monitor your agents activity.",
    "categories": ["Productivity", "Management"],
    "subscriptions": [{
        "name": "Trial",
        "price": 0
      },
      {
        "name": "Professional",
        "price": 3000
      }
    ]
  },
  {
    "id": "b4e7899b-07ba-55b1-9ed3-c38b878623fe",
    "name": "Awesome Calls",
    "description": "Tools to optimize your call center with voice analytics.",
    "categories": ["Optimization", "Voice Analytics"],
    "subscriptions": [{
        "name": "Trial",
        "price": 0
      },
      {
        "name": "Professional",
        "price": 5000
      },
      {
        "name": "Enterprise",
        "price": 10000
      }
    ]
  },
  {
    "id": "d8652502-f8f2-5c35-8de5-b9adfebbf4cf",
    "name": "Scripted",
    "description": "Help your agents communicate with customers using scripts.",
    "categories": ["Productivity", "Optimization"],
    "subscriptions": [{
        "name": "Trial",
        "price": 0
      },
      {
        "name": "Professional",
        "price": 4000
      }
    ]
  }
];
json.sort((a, b) => {
  let sumA = a['subscriptions'].reduce((accumulator, current) => {
    return accumulator + current['price'];
  }, 0);
  let sumB = b['subscriptions'].reduce((accumulator, current) => {
    return accumulator + current['price'];
  }, 0);
  return sumA - sumB;
});
let categories = json.reduce((accumulator, current) => {
  return accumulator.concat(current['categories']).filter((value, index, self) => {
    return self.indexOf(value) === index;
  });
}, []).sort();
console.log(json);
console.log(categories);