3

I have two arrays

var arr1 = ['wq','qw','qq'];
var arr2 = ['wq','wq','wq','qw','qw','qw','qw','qq','qq'];

Below what i did is matching arr1 values with arr2. If the array contains same values i pushed the values into newArr.

var newArr = [];
for (var i=0;i<arr1.length;i++) {
    newArr[i] = [];
}

for (var i=0;i<arr2.length;i++) {
    for (var j=0;j<arr1.length;j++) {
        if (arr2[i].indexOf(arr1[j]) != -1)
            newArr[j].push(arr2[i]);
    }
}
console.log(newArr[1]); //newArr[0] = ['wq','wq','wq'];//In second output array newArr[1] = ['qw','qw','qw','qw'];

Is there any easy way to solve this without using two for loops. Better i need a solution in javascript

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
htoniv
  • 1,658
  • 21
  • 40
  • can you post the output you are expecting from your example inputs. If its an intersection you are looking for try this: http://stackoverflow.com/a/1885569/2617732 – rdans Mar 18 '15 at 17:32
  • Maybe concat. See: http://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items – Misunderstood Mar 18 '15 at 17:37
  • Actually i getting the output but i am using two for loops. how to code it in simple way – htoniv Mar 18 '15 at 17:44
  • Do you need exact match between arrays or if one element is present in both arrays then it qualifies for the new array? – Code Whisperer Mar 18 '15 at 17:45

3 Answers3

1

Maybe use indexOf():

var count = 0;
for (var i = 0; i < arr1.length; i++) {
    if (arr2.indexOf(arr1[i]) != -1) {
        count++;
        // if you just need a value to be present in both arrays to add it
        // to the new array, then you can do it here
        // arr1[i] will be in both arrays if you enter this if clause
    }
}

if (count == arr1.length) {
    // all array 1 values are present in array 2
} else {
    // some or all values of array 1 are not present in array 2
}
Code Whisperer
  • 1,041
  • 8
  • 16
1

Your own way wasn't totally wrong, you just had to check if the element was index of the array and not of an element in the array.

var arr1 = ['wq','qw','qq'];
var arr2 = ['wq','wq','wq','qw','qw','qw','qw','qq','qq'];
var newArr = [];

for (var i in arr1) {
    newArr[i] = [];
}

for (var i in arr2) {
    var j = arr1.indexOf(arr2[i]);
    if (j != -1) {
        newArr[j].push(arr2[i]);
    }
}

This way you removed the nested for loop and it still gives you the result you asked for.

Thalsan
  • 5,140
  • 1
  • 11
  • 12
  • it doesn't showing me the expected output. See i did't get any output in `console.log(newArr[0])` – htoniv Mar 19 '15 at 12:14
  • My bad, made some last minute changes so forgot to fix it. Editted it, so it should be working. – Thalsan Mar 19 '15 at 13:04
0
var arr1 = ['wq','qw','qq','pppp']; 
var arr2 = ['wq','wq','wq','qw','qw','qw','qw','qq','qq'];

function intersect(a, b) {
var d = {};
var results = [];
for (var i = 0; i d[b[i]] = true;
}
for (var j = 0; j if (d[a[j]])
results.push(a[j]);
}
return results;
}

var result_array = intersect(arr1,arr2);

// result_array will be like you want ['wq','wq','wq'];
Ashok Chandrapal
  • 1,020
  • 7
  • 27