This question already has answers here:
How to find elements in array2 that are not in array1?
(2 answers)
Closed 8 years ago.
I have a array a=[1,2,3,4,5] b=[3,4,5,6,7]
Here i want values of array a [1,2] and array b [6,7] and stored in diff arrays like below.
c=[1,2]
d=[6,7]
Thanks in Advance.
its like a=[chkbx_705_49,chkbx_706_49,chkbx_707_49,chkbx_708_49,chkbx_709_49,chkbx_710_49,chkbx_711_49,chkbx_712_49,chkbx_714_49,chkbx_705_50,chkbx_706_50,chkbx_707_50,chkbx_708_50,chkbx_709_50,chkbx_710_50,chkbx_711_50,chkbx_705_51,chkbx_706_51,chkbx_707_51,chkbx_708_51,chkbx_711_51,chkbx_710_52,chkbx_711_52,chkbx_710_53,chkbx_711_53]
b= [chkbx_705_49,chkbx_705_50,chkbx_705_51,chkbx_705_52,chkbx_705_53,chkbx_706_49,chkbx_706_50,chkbx_706_51,chkbx_706_52,chkbx_706_53,chkbx_707_49,chkbx_707_50,chkbx_707_51,chkbx_708_49,chkbx_708_50,chkbx_708_51,chkbx_709_49,chkbx_709_50,chkbx_710_49,chkbx_710_50,chkbx_711_49,chkbx_711_50,chkbx_711_51,chkbx_712_49]
here i deleted chkbx_710_52,chkbx_711_52,chkbx_710_53,chkbx_711_53 checkbox values from array a
and added chkbx_705_52,chkbx_705_53,,chkbx_706_52,chkbx_706_53 added in array b.
So i want c = chkbx_710_52,chkbx_711_52,chkbx_710_53,chkbx_711_53
d = chkbx_705_52,chkbx_705_53,,chkbx_706_52,chkbx_706_53
When a member of A also exists in B, delete in both:
var a = [1,2,3,4,5];
var b = [3,4,5,6,7];
var c = a.slice();
var d = b.slice();
var len = c.length;
while(len--) {
var idx = d.indexOf(c[len]);
if (idx > -1) {
c.splice(len, 1);
d.splice(idx, 1);
}
}
However, you didn't say whether there are duplicated members, so I assume no and do it in the simplest way, just to give you a thought of solution.
You can get it like below:
var array1 = [1,2,3,4,5];
var array2 = [3,4,5,6,7];
var foo1 = [], foo2=[];
var i = 0;
jQuery.grep(array1, function(el) {
if (jQuery.inArray(el, array2) == -1) foo1.push(el);
i++;
});
jQuery.grep(array2, function(el) {
if (jQuery.inArray(el, array1) == -1) foo2.push(el);
i++;
});
alert(" the difference is " + foo1);
alert(" the difference is " + foo2);
Related
I don't know what must be title for my question, I think it's so complicated. So, I have A array:
["87080207", "87101133", "91140156"]
And B Array:
["97150575", "97150575", "90141063"]
This B array, I put on html select value. Each of them(A and B array) is related. I need to show 87080207,87101133 (A array) when I choose value 97150575 (B array).
I have tried, but it didn't work.This is my code:
var a=[];
var b=[];
var arrayLength = dataComponentValuation.length;
for (var i = 0; i < arrayLength; i++) {
a.push(dataComponentValuation[i].valuated);
b.push(dataComponentValuation[i].valuator);
}
var ajoin = a.join();
var bjoin = b.join();
$('#valuatedEmpCompId_before').val(ajoin);
$('#valuator_before').val(bjoin);
In select, I put a function, this is it:
function emptyValuated() {
var valby = $("#valBy").val(); //chosen value from select
var b_valby = $("#valuator_before").val();
var b_valuated = $("#valuatedEmpCompId_before").val();
if(b_valby != ''){
if(valby != b_valby)
{
$("#valuatedEmpCompId").val('');
}
else{
$("#valuatedEmpCompId").val(b_valuated);
}
}
else{
$("#valuator_before").val(valby);
$("#valuatedEmpCompId").val(b_valuated);
}
}
Help me please...
As suggested, you could use an object as reference to the values of array A.
var arrayA = ["87080207", "87101133", "91140156"],
arrayB = ["97150575", "97150575", "90141063"],
object = Object.create(null);
arrayB.forEach(function (b, i) {
object[b] = object[b] || [];
object[b].push(arrayA[i]);
});
console.log(object);
I guess nowadays the Map object is a perfect solution for these jobs.
var arrayA = ["87080207", "87101133", "91140156"],
arrayB = ["97150575", "97150575", "90141063"],
myMap = arrayB.reduce((p,c,i) => p.has(c) ? p.set(c, p.get(c).concat(arrayA[i]))
: p.set(c,[arrayA[i]])
, new Map());
console.log(myMap.get("97150575"));
console.log(myMap.get("90141063"));
This question already has answers here:
How to get unique values in an array [duplicate]
(20 answers)
Closed 6 years ago.
I have an array
var myArray = ['1','1','2','2','1','3'] // 6 item
Is there any ways I can return the value of 1 and 2 and 3 ONE time when looping?
//example in pseudocode
var getNumber = [];
var count = 0;
var check = 0;
for(var i in myArray)
{
if(getNumber[check] !== myArray[i])
{
getNumber[count] = myArray[i];
count++;
}
else
{
}
}
and advice to follow up my previous code?
thanks
You should use Array.indexOf and Array.push to check and insert values.
var getNumber = [];
for(var i in myArray)
{
if(getNumber.indexOf(myArray[i]) < 0) //See if the number wasn't found already
{
getNumber.push(myArray[i]);
}
else
{
//This number was found before. Do nothing!
}
}
you could do something like :
function leaveDupes(arry){
var newArry = [], keys={};
for(var i in arry){
if(!keys[arry[i]]){
newArry.push(arry[i]);
keys[arry[i]]=true;
}
}
return newArry;
}
console.log(leaveDupes(['1','1','2','2','1','3'] ))
using underscore.js, you can do something like:
newArry = _.uniq(['1','1','2','2','1','3']);
var obj = {};
for (var i = 0; i < myArray.length; i++) {
if (!obj[myArray[i]]) {
obj[myArray[i]] = true;
console.log(myArray[i]); //these are the unique values
}
}
This will work.
var myArray = ['1','1','2','2','1','3']
var getNumber = {};
var retArray = [];
myArray.forEach(function(val){
if(!getNumber[val]){
getNumber[val] = val;
retArray.push(val);
}
});
return retArray
You can use forEach and indexOf array method to find the unique elements.
var myArray = ['1','1','2','2','1','3','4','4','5'];
var uniqueArray =[]; //A new array which will hold unique values
function _unique(myArray){
myArray.forEach(function(item,index){
if(uniqueArray.indexOf(item) ==-1){ //Check if new array contains item
uniqueArray.push(item)
}
})
}
_unique(myArray);
This question already has answers here:
Declare variables programmatically?
(3 answers)
Closed 7 years ago.
A user dynamically chooses a value X. Based on the value, X, I want to create unique empty arrays. How would I go about that?
For example, user chooses 4.
I want the following output:
var array1 = [];
var array2 = [];
var array3 = [];
var array4 = [];
Is there a way to properly doing this?
You can specify the name of a property on an object using the square brackets with a string input:
var obj = {};
var numberOfArrays = 4;
for(var i = 1; i <= numberOfArrays; i++){
obj['array' + i] = []; // Specify the name of the array property
}
console.log(obj); // Logs an object which has 4 empty arrays
Just to complement Steven`s answer, you could also create an array of arrays:
var numberOfArrays = X;
var arr = [];
for(var i = 0; i < numberOfArrays; i++){
arr.push(new Array());
}
This question already has answers here:
How to count duplicate value in an array in javascript
(35 answers)
Closed 7 years ago.
I have this JavaScript array with length 129.
var fullnames = [Karri, Ismo, Grigori, Ahmed, Roope, Arto .....]
I would like to find how many times those names appeared in an array and store that information in an array like this:
var counter = [2, 5, 7, ..]
where Karri occured in fullnames array 2 times, Ismo occured 5 times etc. Any ideas about how to do it?
This is the best - and simple - way I can think of:
var fullnames = ["Karri", "Ismo", "Grigori", "Ahmed", "Roope", "Ahmed", "Karri", "Arto", "Ahmed"];
var counts = {};
for (var i = 0; i < fullnames.length; i++)
{
if (!counts.hasOwnProperty(fullnames[i]))
{
counts[fullnames[i]] = 1;
}
else
{
counts[fullnames[i]]++;
}
}
console.log(counts);
Original Fiddle.
Using an array to store the counts doesn't makes much sense, so I used an object instead.
I am assuming that fullnames is array of strings. If so, you can do it like so:
var occurences = { };
for (var i = 0; i < fullnames.length; i++) {
if (typeof occurences[fullnames[i]] == "undefined") {
occurences[fullnames[i]] = 1;
} else {
occurences[fullnames[i]]++;
}
}
console.log(occurences); // Prints out something like: {"Karri": 2, "Ismo": 5, ...}
var fullnames = ['Karri', 'Ismo', 'Grigori', 'Karri', 'Ismo', 'Grigori', 'Grigori', 'Karri', 'Ismo', 'Grigori', 'Grigori'];
var counts = [];
fullnames.forEach(function(_item) {
if(typeof counts[_item] === 'undefined') counts[_item] = 1;
else counts[_item]++;
});
var result = [];
for(i in counts) result.push(counts[i]);
console.log(result);
// outputs [3, 3, 5]
This question already has answers here:
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 9 years ago.
I have two arrays in javascript:
var array1 = ["a","b","c"];
var array2 = ["e","f","g"];
And I want the resulting array to be like this:
array3 = ["a","e","b","f","c","g"];
Any way to do this?
Will a straightforward loop do it?
array3 = new Array();
for(var i = 0; i < array1.length; i++)
{
array3.push(array1[i]);
array3.push(array2[i]);
}
You can try with concat() method:
var array1 = ["a","b","c"];
var array2 = ["e", "f","g"];
var array3 = array1.concat(array2); // Merges both arrays
For your specific requirement, you have to follow this:
function mergeArrays(a, b){
var ret = [];
for(var i = 0; i < a.length; i++){
ret.push(a[i]);
ret.push(b[i]);
}
return ret;
}
This should work:
function zip(source1, source2){
var result=[];
source1.forEach(function(o,i){
result.push(o);
result.push(source2[i]);
});
return result
}
Look http://jsfiddle.net/FGeXk/
It was not concatenation, so the answer changed.
Perhaps you would like to use: http://underscorejs.org/#zip