Comparing user array strings to list empty users [duplicate] - javascript

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;
});
});

Related

Converting a dynamic array into single object [duplicate]

This question already has answers here:
How do I convert array of Objects into one Object in JavaScript?
(17 answers)
Closed 2 years ago.
I have a dynamic array which is like this.
var callData = [
{
"FIELD_1": "0763454333"
},
{
"FIELD_2": "dgfdgfg"
},
{
"FIELD_3": "fgfdgfdg"
}
];
According to the user, these number of fields changes. If another user has more fields, there can be FIELD_4, FIELD_5 and so on. I fetch these data from db and put it into and array as above. Now I want to convert it a single object. I want it to look like this.
{
"FIELD_1": "076355998", "FIELD_2": "933504395v", "FIELD_3": "123"
}
Although I found converting solutions in stackoverflow, Those didn't solve my problem. How can I achieve this? Please guide.
use flatMap function to return entries of each object and then use Object.fromEntries function to create an object from the entries
var callData = [
{"FIELD_1": "0763454333" },
{ "FIELD_2": "dgfdgfg" },
{ "FIELD_3": "fgfdgfdg" }
];
const res = Object.fromEntries(callData.flatMap(o => Object.entries(o)));
console.log(res);

Remove duplicates from array within array in Javascript [duplicate]

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.

Select all JSONObjects from JSONArray containing [duplicate]

This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 8 years ago.
I have a JSONArray containing JSONObjects like this {firstname:'John', lastname:'Doe'}.
Is there a way to select all JSONObjects having firstname == 'John' directly or the only way is to loop the array, test the field firstname and save all matching objects in another array?
Thanks
The filter method should do the trick.
var jsonArrayString = '[{"firstname": "John","otherProp": "otherValue"},{"firstname": "other Name", "otherProp": "otherValue"}]';
var jsonArray = JSON.parse(jsonArrayString);
var filteredArray = jsonArray.filter(function(element) {
return element.firstname === "John";
});
console.log(filteredArray);
Advice: The filter method is not supported in <= IE 8. but there is a polyfill in the MDN article.
You can filter them out:
var people = JSONArray.filter(function(obj) {
return obj.firstname == "John";
});
FYI: You have an array containing objects.

Sort an array in Javascript/JQuery [duplicate]

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);
});

add array value to key javascript [duplicate]

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);

Categories

Resources