sort entries of a drop down alphabitically - javascript

I am dynamically adding drop entries to a drop down by using following code.
var x =document.getElementById("x");
var optn = document.createElement("OPTION");
optn.text="hhh";
optn.value="val";
x.add(optn);
The above thing is done in a loop
Now i want to sort it alphabatically.How can i do it?

It would be easier to sort the array before adding the options to the dropdown. Moving DOM elements around once they are added would be less efficient. For example:
var arr = [ { id: '1', value: 'B' },
{ id: '2', value: 'A' },
{ id: '3', value: 'C' } ];
arr.sort(function(a, b) {
if (a.value < b.value) {
return -1;
}
if (a.value > b.value) {
return 1;
}
return 0;
});
for (var i = 0; i < arr.length; i++) {
// TODO: the array is now sorted => build the dropdown here
}

public void SortDropDownList(DropDownList ddl)
{
//create a ListItem array the size of the items
//in your DropDownList
ListItem[] sorted = new ListItem[ddl.Items.Count];
//loop through all the items in your ListItem array
for (int i = 0; i < sorted.Length; i++)
{
//resize the array on each iteration
Array.Resize(ref sorted, i);
//add the current index to the array
sorted[i] = ddl.Items[i];
}
//call Array.Sort to sort your ListItem array
Array.Sort(sorted);
//remove all items from the DropDownList
ddl.Items.Clear();
//add the sorted items to the DropDownList
ddl.Items.AddRange(sorted);
}
u use the help of this function after the all items bound to the dropdownlist

Related

Compare two arrays with objects and show which item is deleted

I have two arrays which I want to compare and check if there is an deleted item in one of these arrays. If there is show me the difference (deleted item)
Here is the code below how I would like to achieve this:
function compareFilters (a1, a2) {
var a = [], diff = [];
for (var i = 0; i < a1.length; i++) {
a[a1[i]] = true;
}
for (var i = 0; i < a2.length; i++) {
if (a[a2[i]]) {
delete a[a2[i]];
} else {
a[a2[i]] = true;
}
}
for (var k in a) {
console.log('k', k);
diff.push(k);
}
return diff;
}
console.log(filters);
console.log(filters, queryBuilderFilters, compareFilters(queryBuilderFilters, filters));
This will log both arrays which look like this:
[
0: {
id: "AggregatedFields.ReturnOnAdSpend",
label: "ROAS (Return on Ad Spend)",
type: "integer",
description: "The ROAS (Return on Ad Spend)."
},
1: {
id: "AggregatedFields.ROIExcludingAssisted",
label: "ROI excl. assisted",
type: "integer",
description: "The ROI excluding any assisted values.
}
]
And the output of the compareFilters function is 0: "[object Object]"
How can I return the label of the object in this function?
This example illustrates what you want
var completedList = [1,2,3,4,7,8];
var invalidList = new Set([3,4,5,6]);
// filter the items from the invalid list, out of the complete list
var validList = completedList.filter((item) => {
return !invalidList.has(item);
})
console.log(validList); // Print [1,2,7,8]
// get a Set of the distinct, valid items
var validItems = new Set(validList);
You can try this, supposing that the objects in each array have the same reference and are not copies:
function compareFilters (a1, a2) {
const a1l = a1.length;
const a2l = a2.length;
// Both arrays are considered to be equal
if(a1l === a2l) return;
let completed;
let unCompleted;
let deletedValue;
if(a1l > a2l) {
completed = a1;
unCompleted = a2;
} else {
completed = a2;
unCompleted = a1;
}
for(let i = 0; i < completed.lenth; i++) {
if(completed[i] !== unCompleted[i]) {
return completed[i].label;
}
}
}
It will return undefined in case both arrays has the same quantity of elements. Otherwise, it will return the label of first element that is in one array but not the another.

Remove the duplicate values before pushing into an array

It's visible in the image that the array[0] and array[3], array1 and array[4] are same. I was checking why the values are duplicating but I failed. So thought to just remove if any duplicate values exists. I need to remove it from
$scope.arr array itself.
Code:
$scope.arr = [];
planReq.then(function (callplanList) {
$scope.callplanList = callplanList.data.callplans;
//console.log($scope.callplanList);
for(var i = 0; i < $scope.planMapping.length; i++){
//console.log($scope.planMapping[i].PlanScode);
for(var j = 0; j < $scope.callplanList.length; j++){
if(($scope.planMapping[i].PlanScode == $scope.callplanList[j].un_s_code) && ($scope.callplanList[j].offer_type == "REC")){
//console.log($scope.devicesList);
for(var a = 0; a < $scope.callplanList[j].upfront_cost.length; a++){
if($scope.callplanList[j].upfront_cost[a].upfront != ""){
//console.log($scope.callplanList[j].upfront_cost[a].handsetClass);
for(var k = 0; k < $scope.devicesList.length; k++){
if($scope.callplanList[j].upfront_cost[a].handsetClass == $scope.devicesList[k].device_class.toLowerCase()){
$scope.arr.push($scope.devicesList[k]);
}
}
}
}
}
}
}
console.log($scope.arr);
});
Any help would be appreciated.
Make use of filter.
Here is a simple filter, to remove duplicates from an array
Array.filter(function(elem, index, self){ return self.indexOf(elem) == index })
In your case it will be
$scope.arr = $scope.arr.filter(function(elem, index, self){
return self.indexOf(elem) == index
});
method 1:
var list =[{name:"a",age:2}, {name:"b",age:4}, {name:"c",age:6}, {name:"a",age:2}]
var arr = list.filter((elem, index, self) => self.findIndex(
(t) => {return (t.name === elem.name && t.age === elem.age)}) === index)
it return unique array based on all the properties
method 2:
If you want to remove duplicate elements from an array use the following function. Here the args are:
myArr: contain array ob objects
prop: the property of object by which the array elements should be removed
function removeDuplicates(myArr, prop) {
return myArr.filter((obj, pos, arr) => {
return arr.map(mapObj => mapObj[prop]).indexOf(obj[prop]) === pos;
});
}
It will remove the duplicates based on the elements property and return the array with unique elemnts.
ngRepeat uses $watchCollection to detect changes in the collection. When a change happens, ngRepeat then makes the corresponding changes to the DOM:
When an item is added, a new instance of the template is added to the DOM.
When an item is removed, its template instance is removed from the DOM.
When items are reordered, their respective templates are reordered in the DOM.
To minimize creation of DOM elements, ngRepeat uses a function to "keep track" of all items in the collection and their corresponding DOM elements. For example, if an item is added to the collection, ngRepeat will know that all other items already have DOM elements, and will not re-render them.
The default tracking function (which tracks items by their identity) does not allow duplicate items in arrays. This is because when there are duplicates, it is not possible to maintain a one-to-one mapping between collection items and DOM elements.
If you do need to repeat duplicate items, you can substitute the default tracking behavior with your own using the track by expression.
so you can do like this :
<div ng-repeat="n in [42, 42, 43, 43] track by $index">
{{n}}
</div>
If you want to remove duplicates from array :
function UniqueArraybyId(collection, keyname) {
var output = [],
keys = [];
angular.forEach(collection, function(item) {
var key = item[keyname];
if(keys.indexOf(key) === -1) {
keys.push(key);
output.push(item);
}
});
return output;
};
planReq.then(function (callplanList) {
$scope.callplanList = callplanList.data.callplans;
//console.log($scope.callplanList);
for(var i = 0; i < $scope.planMapping.length; i++){
//console.log($scope.planMapping[i].PlanScode);
for(var j = 0; j < $scope.callplanList.length; j++){
if(($scope.planMapping[i].PlanScode == $scope.callplanList[j].un_s_code) && ($scope.callplanList[j].offer_type == "REC")){
//console.log($scope.devicesList);
for(var a = 0; a < $scope.callplanList[j].upfront_cost.length; a++){
if($scope.callplanList[j].upfront_cost[a].upfront != ""){
//console.log($scope.callplanList[j].upfront_cost[a].handsetClass);
for(var k = 0; k < $scope.devicesList.length; k++){
if($scope.callplanList[j].upfront_cost[a].handsetClass == $scope.devicesList[k].device_class.toLowerCase()){
$scope.arr.push($scope.devicesList[k]);
}
}
}
}
}
}
}
$scope.arr = UniqueArraybyId($scope.arr ,"sub_family"); //you have to pass the key name
console.log($scope.arr);
})
Here is the fiddle :
function UniqueArraybyId(collection, keyname) {
var output = [],
keys = [];
angular.forEach(collection, function(item) {
var key = item[keyname];
if(keys.indexOf(key) === -1) {
keys.push(key);
output.push(item);
}
});
return output;
};
function test () {
var arr =[{sub_family:'j3 (2016)'},{sub_family:'j3 (2016)'},{sub_family:'j3 (2017)'}]
arr= UniqueArraybyId(arr ,"sub_family"); //you have to pass the key name
console.log(arr);
};
test();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
I think this is the best way to remove duplicates from an Array. Note that this requires ES6.
arr = Array.from(new Set(arr));
Example:
var arr = [0, 1, 1, 1, 1, 2, 3, 4, 3, 4, 2, 2, 4];
console.log(arr.join(", "));
arr = Array.from(new Set(arr));
console.log(arr.join(", "));

Decrement ID in JSON array after deletion

I have an array stored in my local storage. It is dynamic. I'm storing the data along with an ID which gets incremented after every entry. User has an option of deleting, hence, I'm supposed to remove the corresponding element from the array. But, I want the ID to remain in ascending order. Ex:
var jsonObj = [{'id':'1','name':'Ray','email':'ray#gmail.com'},
{'id':'2','name':'Steve','email':'steve#gmail.com'},
{'id':'3','name':'Albert','email':'albert#gmail.com'},
{'id':'4','name':'Christopher','email':'chris#gmail.com'}]
I'm creating HTML divs for the above array. In case, Steve deletes his details, I want the array to be like:
var jsonObj = [{"id":1,"name":"Ray","email":"ray#gmail.com"},
{"id":2,"name":"Albert","email":'albert#gmail.com"},
{"id":3,"name":"Christopher","email":"chris#gmail.com"}]
The following code doesn't work accordingly.
for (var i=0; i<jsonObj.length; i++) {
//delete function is working fine.
jsonObj[i].id--;
break;
}
You could just iterate from the given index and decrement the id property.
function deleteItem(i) {
array.splice(i, 1);
while (i < array.length) {
array[i].id--;
i++;
}
}
var array = [{ id: '1', name: 'Ray', email :'ray#gmail.com'}, { id: '2', name: 'Steve', email: 'steve#gmail.com' }, { id: '3', name: 'Albert', email: 'albert#gmail.com' }, { id: '4', name: 'Christopher', email: 'chris#gmail.com' }];
deleteItem(1);
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you start from 0, then you do not even need the ID
Ray is the 0th element, Christopher is 3rd
delete Albert and Christopher is 2nd
var jsObj = [{'name':'Ray','email':'ray#gmail.com'},
{'name':'Steve','email':'steve#gmail.com'},
{'name':'Albert','email':'albert#gmail.com'},
{'name':'Christopher','email':'chris#gmail.com'}]
for (var i=0;i<jsObj.length;i++) {
document.write("<br>ID"+(i+1)+": "+jsObj[i].name)
}
document.write("<hr/>");
jsObj.splice(2, 1); // Bye bye Albert
for (var i=0;i<jsObj.length;i++) {
document.write("<br>ID"+(i+1)+": "+jsObj[i].name)
}
More information
Reindex javascript array / object after removing a key
You just declare a new variable in your for loop which you will increment it and you assign this value as their id
for (var i=0, id=1; i<jsonObj.length; i++, id++) {
var jsonObj = [{'id':'1','name':'Ray','email':'ray#gmail.com'},
{'id':'2','name':'Steve','email':'steve#gmail.com'},
{'id':'3','name':'Albert','email':'albert#gmail.com'},
{'id':'4','name':'Christopher','email':'chris#gmail.com'}];
console.log(jsonObj);
jsonObj.splice(1, 1);
for (var i=0, id=1; i<jsonObj.length; i++, id++) {
jsonObj[i].id = id;
}
console.log(jsonObj);
a very simplistic approach could be:
// the index of the deleted element
const delIndex = 2;
// reindex all entries starting from deleted one
for (var i=delIndex+1; i<jsonObj.length; i++) {
jsonObj[i].id = i + 1;
}
The id basically corresponds with the array index anyway. So instead of trying to compute the id anew, we can just overwrite it with the respective index (+1 as you start with one and not zero like array indices).
for your requirements you have to use the splice method in the javascript
array.splice(index_you_wantto_delete,count)
ex:-jsonObj.splice(1,1);
The splice() method adds/removes items to/from an array,
Here is a verbose solution explaining the process step by step
1- Delete the element
2 - Update the indexes if the element was found and deleted
/**
*
* #param array list Your list
* #param int elementID The id of the element you want to remove
* #returns list The list with the element removed and the indexes rearanged
*/
var deleteElement = function (list, elementID) {
var removedIndex = false;
for (var index = 0; index < list.length; index++) {
if (list[index]['id'] === elementID) {
list.slice(index, 1);
removedIndex = index;
break;
}
}
if (removedIndex !== false) {
//Get last id
var lastElement = (removedIndex === 0) ? null : list[removedIndex - 1];
// Set to 0 if the first element was removed
var lastID = (lastElement === null) ? 0 : lastElement.id;
for (var i = removedIndex; i < list.length; i++) {
list[i].id = ++lastID;
}
}
return list;
};
Try the below method. Hope it works !!
// index of the deleted item
var itemDeleted = 2;
// create a for loop from deleted index till last
for (var i = itemDeleted-1; i < jsonObj.length; i++) {
jsonObj[i].id = i+1;
}
You can do in pure js by using map
const jsonObj = [{
'name': 'Ray',
'email': 'ray#gmail.com'
},
{
'name': 'Steve',
'email': 'steve#gmail.com'
},
{
'name': 'Albert',
'email': 'albert#gmail.com'
},
{
'name': 'Christopher',
'email': 'chris#gmail.com'
}
];
jsonObj.splice(1, 1);
const newObj = jsonObj.map((c, i) => ({
name: c.name,
email: c.email,
id: i + 1
}));
console.log(newObj);
Get the deleted index and assign it to the i value of for loop
for (var i=deletedIndex; i<jsonObj.length; i++) {
jsonObj[i].id--;
}

Javascript: Write a function that takes in an array, and then returns an array with only unique numbers, only arrays removed

Write a function that takes in a list and returns a list with all of the duplicates removed (list will only have unique numbers).
Here's what I have so far:
var lista = [1,4,5,1,1,3,5,6,4,4,3];
function dupRemove (lista) {
//Sort the array in case it isn't sorted
lista.sort();
//Object to store duplicates and unique numbers
var listNumbers = {
"Duplicate Numbers": [],
"Unique Numbers": []
};
for (var i = 0; i < lista.length; i++) {
//check if it is not equal to the index of the array before it and after. if it isn't, that means its unique, push it in the uniques array.
if (lista[i] !== lista[i-1] && lista[i] !== lista[i+1]) {
listNumbers["Unique Numbers"].push(lista[i]);
} else {
listNumbers["Duplicate Numbers"].push(lista[i]);
}
}
return listNumbers;
}
Currently, my solution returns an object with keys with the values of "Duplicates": 1, 1, 1, 3, 3, 4, 4, 4, 5, 5 and "Uniques": 6.
How do I remove the duplicates from duplicates and then join these two keys into a single array?
Thank you.
that answer is seriously over -engineered- all you need to to is push all values into a new array if they are not already in it.
function=removeDups()
{
var lista = [1,4,5,1,1,3,5,6,4,4,3];
var uniqueValues=[];
var duplicateValues=[];
for(i=0;i<lista.length;i++)
{
if(uniqueValues.indexof(lista[i] == -1){uniqueValues.push(lista[i]}else{duplicateValues.push(lista[i]}
}
}
You could just use the default filter method that is on all Arrays
You don't need the sort function either. If the item is already found using the indexOf method it will not be added to the newly returned array created by the filter method
var list = [1,4,5,1,1,3,5,6,4,4,3];
function removeDup (arr) {
return arr.filter(function(item, pos) {
return arr.indexOf(item) == pos;
})
}
var sortedList = removeDup(list).sort(function(a,b){
return a - b
})
document.getElementsByTagName('div')[0].textContent = sortedList
<div></div>
Kind of a non elegant solution but it gives you the two arrays: one with the duplicate values and one with the unique ones. Since you cannot rely on .sort() you can just count things.
Function checkList will give you back those two arrays.
var list = [1,4,5,1,1,3,5,6,4,4,3];
console.log(checkList(list));
function checkList(list) {
var uniques = []; // will be [6]
var dups = []; // will be [1, 4, 5, 3]
var checked = []; // save what you have already checked so far
for(i = 0; i < list.length; i++) {
if(notChecked(list[i], checked)) {
checked.push(list[i]);
if(count(list[i], list) > 1) {
dups.push(list[i]);
} else {
uniques.push(list[i]);
}
}
}
return {dups: dups, uniques: uniques}
}
// count how many num in arr
function count(num, arr) {
var count = 0;
var i;
for(i = 0; i < arr.length; i++) {
if(arr[i] == num) count++;
if(count > 1) return count;
}
return count;
}
// check if num has not been checked
function notChecked(num, arr) {
return (arr.indexOf(num) == -1) ? true : false;
}

Convert array to object with saving original index field

I have an array that is sorted in the following way
var items = [2.99, 5.99, 23.99, 1.99];
items.sort(function(a,b) { return a - b;});
This outputs the following:
[1.99, 2.99, 5.99, 23.99]
But I need a way to sort it but keep an index of the original index, e.g.
[3: 1.99, 0: 2.99, 1: 5.99, 2:23.99]
Any help would be appreciated.
Map it to an array of objects.
So in the resulting array, each member is an object with an n property that holds the number, and an i property that holds the original index.
You can then iterate that array and get the data like normal.
var items = [2.99, 5.99, 23.99, 1.99];
var arr_of_objs = items.map(function(n, i) {
return { n:n, i:i };
}).sort(function(a, b) {
return a.n - b.n;
});
arr_of_objs.forEach(function(obj, i) {
this.textContent += "number: " + obj.n + ", orig idx: " + obj.i + ", new idx: " + i + "\n";
}, document.querySelector("pre"));
<pre></pre>
var items = [2.99, 5.99, 23.99, 1.99];
var sortable = [];
for (var i = 0; i < items.length; i++) {
sortable.push([i, items[i]]);
}
sortable.sort(function(a, b) {
return a[1] - b[1]
});
console.log(sortable);
Unfortunately it is not possible in JS get sorting in this case. JS understands array only as [0: 1.99, 1:2.99, 3:23.99] -- you cannot change order of indexes. But you can use array of arrays or array of object to solve the problem.
var items = [2.99, 5.99, 23.99, 1.99];
function PreserveKeysSorting(arr) {
var arr = arr
obj = [];
for(i in arr) {
obj.push({index: i, value: arr[i]});
}
obj.sort(function(a,b) { return a.value - b.value;});
return obj;
}
console.log(items);
console.log(PreserveKeysSorting(items));
Live Demo -- http://jsfiddle.net/u1g0xsap/1/
The array that you want as a result is not valid, but you can do something like this:
First make an array that contains objects that persists the index:
var itemsObj = [];
items.forEach(function(value, index) {
itemsObj.push({
value: value,
index: index
});
});
Then you can sort them like this:
items.sort(function(a,b) { return a.value - b.value;});
and you will get an arary of objects like this
[{index:3, value: 1.99}, {index: 0, value: 2.99}, {index: 1, value: 5.99}, {index:2, value:23.99}]
Hardcode the index into the array:
var items = [2.99, 5.99, 23.99, 1.99]
var itemsWithIndex = [];
for (i=0; i<items.length; i++) { itemsWithIndex[i] = [i+1, items[i]]; }
itemsWithIndex.sort(function(a,b) { return a[1]-b[1]; });
Note that an auxiliary variable (itemsWithIndex) was added for clarity, but you can also just update the original array:
var items = [2.99, 5.99, 23.99, 1.99]
for (i=0; i<items.length; i++) { items[i] = [i+1, items[i]]; }
items.sort(function(a,b) { return a[1]-b[1]; });
You could use two arrays and indexOf() method:
// Variables
var items = items_sort = [2.99, 5.99, 23.99, 1.99];
var order = new Array();
// Sort array
items_sort.sort(function(a,b) { return a - b;});
// Get order
for (i = 0; i < items.length; i++) {
order[i] = items.indexOf(items_sort[i]);
}

Categories

Resources