如何在ionic 2中对JSON数组中的唯一JSON对象进行排序

时间:2018-04-03 04:58:32

标签: ionic-framework ionic2 ionic3 ionic-native

我是Ionic 2的初学者。我已成功将URL中的数据提取到数组中。在我的json响应中,有多个重复的 JSON对象。我需要对这个json对象进行排序,并需要在 accordion-list

中显示

这是我的json回复:

{
    "result": [
        {
            "ws_type": "Speed Boat",
            "ws_district": "Thane"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Thane"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Thane"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Thane"
        },
        {
            "ws_type": "Parasailing",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Kayaking",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Rubber Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride ",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Kayaking",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride ",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Motar Lounch",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Yacht",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Rubber Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Rubber Boat",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Raigad"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Banana ride",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Speed Boat ",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "ATV Ride",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Ratnagiri"
        },
        {
            "ws_type": "WaterScooter",
            "ws_district": "Sindhudurg"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Sindhudurg"
        },
        {
            "ws_type": "Jet ski",
            "ws_district": "Sindhudurg"
        },
        {
            "ws_type": "WaterScooter",
            "ws_district": "Sindhudurg"
        },
        {
            "ws_type": "Speed Boat",
            "ws_district": "Sindhudurg"
        },
        {
            "ws_type": "Parasailing",
            "ws_district": "Sindhudurg"
        },
        {
            "ws_type": "WaterScooter",
            "ws_district": "Sindhudurg"
        },
        {
            "ws_type": "Kayaking",
            "ws_district": "Sindhudurg"
        },
        {
            "ws_type": "Banana ride ",
            "ws_district": "Sindhudurg"
        },
        {
            "ws_type": "Bumper ride",
            "ws_district": "Sindhudurg"
        },
        {
            "ws_type": "Parasailing",
            "ws_district": "Sindhudurg"
        }
    ]
}

这是我的代码

我正在使用以下代码对json对象进行排序,但我没有得到所需的结果:

var  result = _.values((this.information.reduce((r,o) => {
            r[o.ws_type] = r[o.ws_type] ||  r[o.ws_district] ||{ws_type: o.ws_type, children: []};
            //r[o.ws_district] = r[o.ws_district] ||  r[o.ws_district] ||{ws_district: o.ws_district, children: []};
            r[o.ws_type].children.push( r[o.ws_district] ||  r[o.ws_district] ||{ws_district: o.ws_district});
            return r;
          },{})));

这是我的输出 enter image description here

所需输出

我想在我的列表中只有一次Raigad区。这是因为我的结果JSON数组中有多个重复的json对象

必填结果:

- 快艇

  • 赖加德
  • 勒德纳吉里
  • Sindhudurg
  • 塔内

谢谢。请帮忙解决这个问题。

2 个答案:

答案 0 :(得分:0)

使用过滤方法,我更新了我的答案 推荐链接:https://jsfiddle.net/rv6d1byr/

let uniquedata = {}
let fruits = [
    {
        "ws_type": "Speed Boat",
        "ws_district": "Thane"
    },
    {
      "ws_type": "Bumper ride",
       "ws_district": "Thane"
    },
    {
        "ws_type": "Bumper ride",
      "ws_district": "Thane"
    }

]
let filtered = fruits.filter(obj => !uniquedata[obj.ws_type] && (uniquedata[obj.ws_type] = true));

 console.log(filtered);

答案 1 :(得分:0)

使用reduce方法将其过滤

 let result = results.reduce((cur,prev)=>{
      const type = prev['ws_type'].replace(/(^\s+|\s+$)/g, '')
      if(cur.hasOwnProperty(type)){
        if(cur[type].indexOf(prev['ws_district']) == -1){
          cur[type].push(prev['ws_district'])
        }
      }else{
        cur[type] = [prev['ws_district']]
      }
      return cur
    },{})
console.log(result)
console.log(result['Speed Boat'])

请参阅此https://stackblitz.com/edit/ionic-fu91dt?file=pages%2Fhome%2Fhome.ts