首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用JSON和Javascript的复杂过程(ES6)

使用JSON和Javascript的复杂过程(ES6)
EN

Stack Overflow用户
提问于 2018-07-06 08:57:35
回答 1查看 46关注 0票数 -3

好的,我必须使用这个JSON:

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

和:

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

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

非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-06 09:23:44

试一试。

代码语言:javascript
复制
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);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51201722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档