Jquery to compare and return string arrays - javascript

Hi I am developing one jquery application. I am trying to compare the two arrays. For example,
Firstarray=["Mike","Jack"];
SecondArray=["Mike","Jack","Andy","Cruz"];
Whenever we compare above two arrays I want to return the strings which exists in both arrays or which are common to both arrays!
I tried as below. This piece of code is not working.
for (var i = 0; i < Firstarray.length; i++) {
for (var j = 0; j < SecondArray.length; j++) {
if (Firstarray[i] == SecondArray[j]) {
alert('found ' + SecondArray[j]);
return;
}
}
}
Can anyone help me in this regards! Thank you very much.

You can use indexOf() function
Firstarray=["Mike","Jack"];
SecondArray=["Mike","Jack","Andy","Cruz"];
var result = new Array();
for (var i = 0; i < Firstarray.length; i++) {
if(SecondArray.indexOf(Firstarray[i])>=0){
result.push(Firstarray[i]);
}
}
console.log(result);

Here is a solution using Array.prototype.filter and Array.prototype.some along with some ES6 flavor thrown in - see demo below:
var firstArray=["Mike","Jack"];
var secondArray=["Mike","Jack","Andy","Cruz"];
var result = secondArray.filter(a => firstArray.some(b => a === b));
console.log(result);

check this How can I find matching values in two arrays?
Array.prototype.diff = function(arr2) {
var ret = [];
this.sort();
arr2.sort();
for(var i = 0; i < this.length; i += 1) {
if(arr2.indexOf( this[i] ) > -1){
ret.push( this[i] );
}
}
return ret;
};

var FirstArray=["Mike","Jack"];
var SecondArray=["Mike","Jack","Andy","Cruz"];
var commonArray = Array();
var count=0;
for (var i=0; i<FirstArray.length; i++) {
for (var j=0;j< SecondArray.length;j++) {
if (FirstArray[i] == SecondArray[j]){
commonArray[count]=FirstArray[i];
count++;
}
}
}
console.log(commonArray);

Try changing few things in your code :
var Firstarray=["Mike","Jack"];
var SecondArray=["Mike","Jack","Andy","Cruz"];
var matchedData = [];
for (var i = 0; i < Firstarray.length; i++) {
for (var j = 0; j < SecondArray.length; j++) {
if (Firstarray[i] == SecondArray[j]) {
matchedData.push(SecondArray[j]);
}
}
}
alert(matchedData);
working fiddle :
https://jsfiddle.net/o3brcsvw/

try this
var Firstarray=["Mike","Jack"];
var SecondArray=["Mike","Jack","Andy","Cruz"];
var matchedData = [];
for (var i = 0; i < Firstarray.length; i++) {
for (var j = 0; j < SecondArray.length; j++) {
if (Firstarray[i] == SecondArray[j]) {
//alert('found ' + SecondArray[j]);
matchedData.push(SecondArray[j]);
}
}
}
return matchedData;

Related

I want to add together the numbers of a nested array

This is what I came up with:
var nestedArr = [[[1,2],[3,4]],[[5,6]]];
function sumTotal() {
for(var i = 0; i < nestedArr.length; i++) {
for(var j = 0; j < nestedArr[i].length; j++) {
for(var k = 0; k < nestedArr[i][j].length; k++) {
var arrNumSum = nestedArr[i][j][k];
arrNumSum += arrNumSum;
return arrNumSum;
}
}
}
}
sumTotal();
You can instead create recursive function using reduce()
var nestedArr = [[[1,2],[3,4]],[[5,6]]];
function sum(arr) {
return arr.reduce(function(r, e) {
return r + (Array.isArray(e) ? sum(e) : e)
}, 0)
}
console.log(sum(nestedArr))
You're overwriting arrNumSum each time through the loop. Moreover, you're returning too soon, right after the first iteration. Try this instead:
var nestedArr = [[[1,2],[3,4]],[[5,6]]];
function sumTotal() {
var arrNumSum = 0;
for(var i = 0; i < nestedArr.length; i++) {
for(var j = 0; j < nestedArr[i].length; j++) {
for(var k = 0; k < nestedArr[i][j].length; k++) {
arrNumSum += nestedArr[i][j][k];
}
}
}
return arrNumSum;
}
console.log(sumTotal());
You could use a recusive call of Array#reduce with a named function as callback.
var array = [[[1, 2], [3, 4]], [[5, 6]]],
total = array.reduce(function add(r, a) {
return Array.isArray(a) ? a.reduce(add, r) : r + a;
}, 0);
console.log(total);

for loop efficiency, make two for loops into one

So I have 2 separate 2D array, they are not necessarily the same length, and I want to make an object for each. I made this:
var obj1 = {},
obj2 = {};
for (var i=0; i<arr1.length; i++) {
obj1[arr1[i][1]] = arr1[i][0];
}
for (var j=0; j<arr2.length; j++) {
obj2[arr2[j][1]] = arr2[j][0];
}
My question is if there is a way to make this with only one loop. Thanks!
You could try something like this:
var obj1 = {},
obj2 = {},
length = Math.max(arr1.length, arr2.length); // get max array length
for (var i = 0; i < length; i++) { // loop max array length
if (i < arr1.length) { // do check for arr1
obj1[arr1[i][1]] = arr1[i][0];
}
if (i < arr2.length) { // do check for arr2
obj2[arr2[i][1]] = arr2[i][0];
}
}
As pointed out, this may be less efficient than 2 separate loops,
Although it also may be more efficient
What you really want here is a function abstraction that removes the duplication. There is nothing to make this more efficient (if you meant time complexity, not developer efficiency):
function arrToObj(arr) {
var obj = {};
for (var i=0; i<arr.length; i++) {
obj[arr[i][1]] = arr[i][0];
}
return obj;
}
var obj1 = arrToObj(arr1),
obj2 = arrToObj(arr2);
The loop is still executed twice, but it's only written once.
Something like this should work:
var obj1 = {},
obj2 = {};
for (let i = 0; i < arr1.length && i < arr2.length; i++) {
obj1[arr1[i][1]] = arr1[i][0];
obj2[arr2[i][1]] = arr2[i][0];
}
if (arr1.length > arr2.length) {
for (let i = arr2.length; i < arr1.length; i++) {
obj1[arr1[i][1]] = arr1[i][0];
}
}
if (arr2.length > arr1.length) {
for (let i = arr1.length; i < arr2.length; i++) {
obj2[arr2[i][1]] = arr2[i][0];
}
}
Inspired by Pete's and eur00t's answers, I suggest this one:
var commonLength = Math.min(arr1.length, arr2.length);
for (var i = 0; i < commonLength; i++) {
obj1[arr1[i][1]] = arr1[i][0];
obj2[arr2[i][1]] = arr2[i][0];
}
for (var i = commonLength; i < arr1.length; i++) {
obj1[arr1[i][1]] = arr1[i][0];
}
for (var i = commonLength; i < arr2.length; i++) {
obj2[arr2[i][1]] = arr2[i][0];
}
As the question is about efficiency, I made a jsperf test case to compare the solutions.
var obj1 = {},
for (var i=0; i<arr1.length; i++) {
obj1[arr1[i][1]] = arr1[i][0];
}
for (var j=0; j<arr2.length; j++, i++) {
obj2[arr2[i][1]] = arr2[j][0];
}
Hope this will you

Get all substrings of a string in JavaScript

I have the following function to get all of the substrings from a string in JavaScript. I know it's not correct but I feel like I am going about it the right way. Any advice would be great.
var theString = 'somerandomword',
allSubstrings = [];
getAllSubstrings(theString);
function getAllSubstrings(str) {
var start = 1;
for ( var i = 0; i < str.length; i++ ) {
allSubstrings.push( str.substring(start,i) );
}
}
console.log(allSubstrings)
Edit: Apologies if my question is unclear. By substring I mean all combinations of letters from the string (do not have to be actual words) So if the string was 'abc' you could have [a, ab, abc, b, ba, bac etc...] Thank you for all the responses.
You need two nested loop for the sub strings.
function getAllSubstrings(str) {
var i, j, result = [];
for (i = 0; i < str.length; i++) {
for (j = i + 1; j < str.length + 1; j++) {
result.push(str.slice(i, j));
}
}
return result;
}
var theString = 'somerandomword';
console.log(getAllSubstrings(theString));
.as-console-wrapper { max-height: 100% !important; top: 0; }
A modified version of Accepted Answer. In order to give the minimum string length for permutation
function getAllSubstrings(str, size) {
var i, j, result = [];
size = (size || 0);
for (i = 0; i < str.length; i++) {
for (j = str.length; j - i >= size; j--) {
result.push(str.slice(i, j));
}
}
return result;
}
var theString = 'somerandomword';
console.log(getAllSubstrings(theString, 6));
Below is a recursive solution to the problem
let result = [];
function subsetsOfString(str, curr = '', index = 0) {
if (index == str.length) {
result.push(curr);
return result;
}
subsetsOfString(str, curr, index + 1);
subsetsOfString(str, curr + str[index], index + 1);
}
subsetsOfString("somerandomword");
console.log(result);
An answer with the use of substring function.
function getAllSubstrings(str) {
var res = [];
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j <= str.length; j++) {
res.push(str.substring(i, j));
}
}
return res;
}
var word = "randomword";
console.log(getAllSubstrings(word));
function generateALlSubstrings(N,str){
for(let i=0; i<N; i++){
for(let j=i+1; j<=N; j++){
console.log(str.substring(i, j));
}
}
}
Below is a simple approach to find all substrings
var arr = "abcde";
for(let i=0; i < arr.length; i++){
for(let j=i; j < arr.length; j++){
let bag ="";
for(let k=i; k<j; k++){
bag = bag + arr[k]
}
console.log(bag)
}
}
function getSubstrings(s){
//if string passed is null or undefined or empty string
if(!s) return [];
let substrings = [];
for(let length = 1 ; length <= s.length; length++){
for(let i = 0 ; (i + length) <= s.length ; i++){
substrings.push(s.substr(i, length));
}
}
return substrings;
}

How can I use data in a string to update a javascript object?

I have the following data array:
var ans =
[
{"text":"x","response":false},
{"text":"y","response":false},
{"text":"z","response":true}
];
var correct = "010"; // I need to add this to the array ans
Can anyone suggest how I could use use the data in the correct variable to add to the array so as to make:
var ans =
[
{"text":"x","response":false,"correct":false},
{"text":"y","response":false,"correct":true},
{"text":"z","response":true,"correct":false}
];
for(var i=0; i< ans.length; i++) {
ans[i].correct = correct.charAt(i) == "1";
}
for (var i = 0; i < correct.length; i++) {
ans[i]["correct"] = correct[i] === "1";
}
You an also do it like this(using a for-in loop).
Reference: For-each over an array in JavaScript?
for(key in ans){
ans[key].correct = correct.charAt(key) == "1";
}
var ans =
[
{"text":"x","response":false},
{"text":"y","response":false},
{"text":"z","response":true}
];
var correct = "010";
var sep = correct.split("");
var arr = [];
for (var i = 0; i < sep.length; i++) {
if (sep[i] == 0) {
arr.push("false");
} else {
arr.push("true");
}
}
var len = ans.length;
for (var i = 0; i < len; i++) {
ans[i].correct = arr[i];
}

removing duplicates from a nested/2D array (removing the nested duplicate element)

So that:
array = [[12,13,24],[24,22,11],[11,44,55]]
would return:
cleanedArray = [[12,13,24],[22,11],[44,55]]
I'm surprised not to have found this answered here.
var array = [[12,13,24],[24,22,11],[11,44,55]];
var output = [];
var found = {};
for (var i = 0; i < array.length; i++) {
output.push([]);
for (var j = 0; j < array[i].length; j++) {
if (!found[array[i][j]]) {
found[array[i][j]] = true;
output[i].push(array[i][j]);
}
}
}
console.log(output);
Are you looking for a function that does this for just two-dimensional arrays? If so, then I think this would work:
Array.prototype.clean = function()
{
var found = [];
for(var i = 0; i < this.length; i++)
{
for(var j = 0; j < this[i].length; j++)
{
if(found.indexOf(this[i][j]) != -1)
{
this[i].splice(j, 1);
}
else
{
found.push(this[i][j]);
}
}
}
return this;
};
If it's just a one-dimensional array you're looking for, then:
Array.prototype.clean = function()
{
var found = [];
for(var i = 0; i < this.length; i++)
{
if(found.indexOf(this[i]) != -1)
{
this.splice(i, 1);
}
else
{
found.push(this[i]);
}
}
return this;
};
this would work. If you're doing either of those, then do .clean() on your array to clean it up.
A simple function that modifies the original array is:
function removeDups(a) {
var item, j, found = {};
for (var i=0, iLen=a.length; i<iLen; i++) {
item = a[i];
j=item.length;
while (j--) {
found.hasOwnProperty(item[j])? item.splice(j,1) : found[item[j]] = '';
}
}
return a;
}

Categories

Resources