add array value to key javascript [duplicate] - javascript

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

Related

How to get unique array with only filter in Javascript [duplicate]

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

Comparing user array strings to list empty users [duplicate]

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

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

Categories

Resources