从json加载唯一数据

时间:2017-08-23 05:11:04

标签: javascript jquery json

我得到了一些json数据,如下所示。

[
    {
        "LocationId": 1,
        "LocationTitle": "L",
        "CountryCode": "ABC",
        "Country": "G"
    },
    {
        "LocationId": 2,
        "LocationTitle": "L 2",
        "CountryCode": "ABC",
        "Country": "G"
    },
    {
        "LocationId": 3,
        "LocationTitle": "L 3",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 4,
        "LocationTitle": "L 4",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 5,
        "LocationTitle": "L 5",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 6,
        "LocationTitle": "L 6",
        "CountryCode": "HIJ",
        "Country": "V"
    }
]

加载唯一国家/地区的最有效方式是什么,让我们说下拉列表?

这样下拉列表只包含3个项目,即G,M和V?

3 个答案:

答案 0 :(得分:0)

您可以使用解决方案https://jsfiddle.net/tL7263L7/

var data = [
    {
        "LocationId": 1,
        "LocationTitle": "L",
        "CountryCode": "ABC",
        "Country": "G"
    },
    {
        "LocationId": 2,
        "LocationTitle": "L 2",
        "CountryCode": "ABC",
        "Country": "G"
    },
    {
        "LocationId": 3,
        "LocationTitle": "L 3",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 4,
        "LocationTitle": "L 4",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 5,
        "LocationTitle": "L 5",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 6,
        "LocationTitle": "L 6",
        "CountryCode": "HIJ",
        "Country": "V"
    }
];

var uData = [];

$.each(data, function(i){
	if(uData.indexOf(data[i].Country) < 0)
  	uData.push(data[i].Country);
})

console.log(uData);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

只需loop通过数据object&amp;使用indexOf中具有唯一国家/地区的array检查重复元素。

答案 1 :(得分:0)

普通Javascript中的解决方案。

&#13;
&#13;
const arrayOfJson = [
    {
        "LocationId": 1,
        "LocationTitle": "L",
        "CountryCode": "ABC",
        "Country": "G"
    },
    {
        "LocationId": 2,
        "LocationTitle": "L 2",
        "CountryCode": "ABC",
        "Country": "G"
    },
    {
        "LocationId": 3,
        "LocationTitle": "L 3",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 4,
        "LocationTitle": "L 4",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 5,
        "LocationTitle": "L 5",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 6,
        "LocationTitle": "L 6",
        "CountryCode": "HIJ",
        "Country": "V"
    }
]

const uniqueCountries = []

arrayOfJson.forEach(val => {
  if (uniqueCountries.indexOf(val.Country) === -1) {
    uniqueCountries.push(val.Country)
  }
})

console.log(uniqueCountries)
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用reduce单行显示。

let ar = [
    {
        "LocationId": 1,
        "LocationTitle": "L",
        "CountryCode": "ABC",
        "Country": "G"
    },
    {
        "LocationId": 2,
        "LocationTitle": "L 2",
        "CountryCode": "ABC",
        "Country": "G"
    },
    {
        "LocationId": 3,
        "LocationTitle": "L 3",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 4,
        "LocationTitle": "L 4",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 5,
        "LocationTitle": "L 5",
        "CountryCode": "EFG",
        "Country": "M"
    },
    {
        "LocationId": 6,
        "LocationTitle": "L 6",
        "CountryCode": "HIJ",
        "Country": "V"
    }
];

console.log(ar.reduce( (p, c) => p.indexOf(c.Country) === -1 ? p.concat([c.Country]) : p, []));