This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 7 years ago.
I started with an array formatted like this:
var cus = {
"acct":[
{
"latitude":"41.4903",
"longitude":"-90.56956",
"part_no":"P1140",
"no_sold":1
},
{
"latitude":"48.118625",
"longitude":"-96.1793",
"part_no":"227",
"no_sold":1
},
....
]
Next I put all of the part_no in a separate array like this:
var list = [];
$.each(cus.acct,function(index,value){
list = [value["part_no"]];
These are the results when I do a console.log() of my array:
["P1140"]
["227"]
["224"]
["600"]
.....
["756"]
["756"]
["756"]
How do I remove duplicates from this array of just part_no's with javascript/jquery? I've looked at other examples but can't find one that works for me. Take note that I'm just beginning with javascript as well.
function getUnique(arr){
var result = [];
$.each(arr, function(i, e) {
if(typeof e != "undefined")
{
if ($.inArray(e, result) == -1) result.push(e)
}
});
return result;
}
If you can use any libraries like underscope or lodash will provide more options.
I would use the jQuery unique option. It should remove any duplicates from your array.
Related
This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 6 years ago.
I have an array:
var a = [2,3,4,5,5,4]
I want to get unique array out of given array like
b = [2,3,4,5]
I have tried
a.filter(function(d){return b.indexOf(d)>-1})
and I don't want to use for loop.
You can simply do it in JavaScript, with the help of the second - index - parameter of the filter method:
var a = [2,3,4,5,5,4];
a.filter(function(value, index){ return a.indexOf(value) == index });
This is not an Angular related problem. It can be resolved in a couple of ways depending which libraries you are using.
If you are using jquery:
var uniqeElements = $.unique([2,3,4,5,5,4]);
The output would be:
[2,3,4,5]
If you are using underscore:
var uniqeElements = _.uniq([2,3,4,5,5,4]);
Same output.
And pure JS code:
var unique = [2,3,4,5,5,4].filter(function(elem, index, self) {
return index == self.indexOf(elem);
})
var b = a.filter( function( item, index, inputArray ) {
return inputArray.indexOf(item) == index;
});
This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 7 years ago.
I want a simple way to compare two arrays. One array has a list of emails and the other array is a list of emails who completed a form. I want to then return a list of people who have not completed the form. Here is the function I have, but it works pretty slow.
function findMissingUsers() {
var sheet = getSheet();
users = [array of all emails];
completedUsers = [array of emails who completed form];
users.forEach(function (row) {
completedUsers.forEach(function (user) {
if(row.Email != user.Username) {
console.log(row);
}
});
});
}
Just trying to find a more efficient way of doing this.
How about:
completedUsers.filter(function(n) {
return users.indexOf(n) != -1
});
from here.
You can use e.g. lodash library and _.difference function. Details here.
You can do it like this:
var incompletedUsers = users.filter(function(user){
return completedUsers.every(function(completedUser){
return user.Email !== completedUser.Username;
});
});
This question already has answers here:
How to sort an array of objects with jquery or javascript [duplicate]
(6 answers)
Closed 8 years ago.
There is an array emails having few attributes To, From, Subject, Description. I retrieve records from database and populate all rows in this array. I use to access all rows as follows:
for (var i = 0; i < emails.length; i++) {
var To = emails[i].To;
var Sender = emails[i].Sender;
var Subject = emails[i].Subject;
var Description = emails[i].Description;
}
Now I need to sort this array alphabetically by To values and store the sorted emails in another array sortedemails. How can I do this in easiest possible way in Javascript/JQuery?
Thanks.
Arrays in javascript have a sort function.
emails.sort(function(a, b) {
if (a.To < b.To) {
return -1;
} else {
return 1;
}
}
JavaScript arrays have a built-in .sort method. No loops (or jQuery needed).
emails.sort(function(a, b){
return a.To.localeCompare(b.To);
});
This will modify the email array. If you want to save the original (unsorted) array, you'll need to copy the array before sorting.
var sortedemails = emails.slice(0);
sortedemails.sort(function(a, b){
return a.To.localeCompare(b.To);
});
This question already has an answer here:
How to filter a javascript object array with variable parameters
(1 answer)
Closed 9 years ago.
I am looking for a way to filter my JavaScript Array() columns where the parentId is equal to a variable passed into the method.
// Array decleration
var columns = []; // Columns
//...
for (var i1 in columns) {
if (columns[i1].parentId == listItem) {
//...
Could anybody recommend the easiest way to filter this using either plain JavaScript or jQuery to avoid using the if statement as shown above?
var filteredColumns = columns.filter(function(column) {
return column.parentId == listItem;
});
array = [1,2,3,4,5];
result = $.grep(array, function(n,i) {
return n > 3;
});
This will return an array of filtered elements where the results are greater than 3. Here n is the element in consideration, and i the index of the element. So as per your requirement, the code can run like this:
resultArray = $.grep(columns,function(n,i) {
return n == parentId;
});
Use ES5 Array's filter method:
var filtered = columns.filter(function (item) {
return item.parentId === listItem
});
In the link above there is also a shim for old browsers.
You can also doing that manually:
var filtered = [];
for (var i = 0, item; item = columns[i++];)
if (item.parentId === listItem) filtered.push(item);
Don't use for…in to iterate over Array.
This question already has answers here:
Add new value to an existing array in JavaScript [duplicate]
(8 answers)
Closed 10 years ago.
i'm getting the data from a form when it is submitted like this
values = {};
$("#myForm").submit(function(){
$.each($('#myForm').serializeArray(), function(i, field) {
if(field.name != 'r'){
values[field.name] = field.value;
}
});
return false;
});
The problem is that i want to do that multiple times and store all the data in the var values using field.name as a keys and the values as an array
to compare it in php i would do values[field.name][] = field.value;
is there any similar syntax in js ?
Yeah, you can add multiple values using the Array.push method. But first, you must define values[field.name] as array, like this:
values[field.name] = [];
values[field.name].push(somevalue);