I have a list of address in local storage stored as a JSON object.
{"Addresses"[
{"address1":"1111 W Main"},
{"address2":"2222 W Main"},
{"address3":"1111 W Main"},
{"adess4":"3333 Main"}
]};
I need to sort them and remove the duplicates.
I can do this with an array
var addressList = ['2111 W State', '2111 W State', '1111 W State', '3111 W State'];
var sortedAddressList = addressList.sort();
var results = [];
for (var i = 0; i < addressList.length; i++) {
if (sortedAddressList[i + 1] != sortedAddressList[i]) {
results.push(sortedAddressList[i]);
}
}
alert(results);
How can I get the same results without converting the object to a string??
First, your object property naming isn't very sensible. The access key should be predictable rather than relying on knowledge of an index.
{"Addresses": [
{"address":"1111 W Main"},
{"address":"2222 W Main"},
{"address":"1111 W Main"},
{"address":"3333 Main"}
]};
So at this point, you can use the technique you were using before, but you simply need to access the "address"
property.
var sortedAddressList = data.Addresses.sort(function(a, b) {
return a.address.localeCompare(b.address);
});
var results = [];
for (var i = 0; i < sortedAddressList.length; i++) {
if (sortedAddressList[i + 1].address != sortedAddressList[i].address) {
results.push(sortedAddressList[i]);
}
}
alert(results);
or we could make it a little more modern:
var results = data.Addresses.sort(function(a, b) {
return a.address.localeCompare(b.address);
}).filter(function(item, i, arr) {
return arr[i + 1].address != item.address;
});
alert(results);
You can read and put unique value into your array like this
var data = {
"Addresses": [{
"address1": "2222 W Main"},
{
"address2": "1111 W Main"},
{
"address3": "1111 W Main"},
{
"adess4": "3333 Main"}]
};
var arr = [];
var address = ['address1', 'address2', 'address3', 'adess4'];
$.each(data.Addresses, function(i, value) {
var val = value[address[i]];
if (arr.indexOf(val) == -1) {
arr.push(val);
}
});
console.log(arr);
arr.sort();
For starters I would make all the keys "address":
{"Addresses"[{"address":"1111 W Main"},{"address":"2222 W Main"},{"address":"1111 W Main"},{"adess":"3333 Main]};
After that, you can loop through the JSON object for that specific key and store those values into an array. Finally, you can use your sort/delete duplicate method from above.
var addressList = {"Addresses":[{"address":"1111 W Main"},{"address":"2222 W Main"},{"address":"1111 W Main"},{"address":"3333 Main"}]};
var arr = new Array();
for(var i =0; i < addressList.Addresses.length; i++){
arr.push(addressList.Addresses[i].address);
}
var sortedAddressList = arr.sort();
var results = [];
for (var i = 0; i < sortedAddressList.length; i++) {
if (sortedAddressList[i + 1] != sortedAddressList[i]) {
results.push(sortedAddressList[i]);
}
}
alert(results);
Is something like this what you're looking for?
var uniqueSortedAddresses = [];
({"Addresses":[
{"address1":"1111 W Main"},
{"address2":"2222 W Main"},
{"address3":"1111 W Main"},
{"address4":"3333 Main"}
]}).Addresses.forEach(function(addressObj) {
var i,address;
for(i in addressObj) {
if(addressObj.hasOwnProperty(i)) {
address = addressObj[i];
if(uniqueSortedAddresses.indexOf(address)==-1){
uniqueSortedAddresses.push(address);
}
}
}
});
uniqueSortedAddresses.sort();
Using Jackson Object mapper, I think you could easily get a Address arraylist with a inputstream or byte[], any types you like.
Simply put it into a java.util.SortedSet and all the things done.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.