logo
down
shadow

JavaScript comparing two arrays


JavaScript comparing two arrays

By : user2948494
Date : November 14 2020, 04:51 PM
wish of those help Is there i way in JavaScript to compare two arrays; , If legacy is not a problem, Something like this would do:
code :
var array1 = ['jpg','png','d'];
var array2 = ['jpg','pdf','png','bmp'];

var result = !array1.filter(function(a) { return array2.indexOf(a)==-1; }).length;   

// result is False


Share : facebook icon twitter icon
comparing javascript arrays

comparing javascript arrays


By : Ebrin
Date : March 29 2020, 07:55 AM
wish of those help Here's a way using a JSON object and no jQuery, although the $.inArray() should work fine:
code :
var imagesInUploadsFolder = [
    '/path/to/img1.png',
    '/path/to/img2.png',
    '/path/to/img3.png'
];
var imagesInDatabaseTable = [
    '/path/to/img1.jpg',
    '/path/to/img2.png',
    '/path/to/img4.png'
];

var database_json = JSON.stringify(imagesInDatabaseTable);

for (var i = 0; i < imagesInUploadsFolder.length; i++) {
    console.log(imagesInUploadsFolder[i] + ' in ' + database_json);
    if (database_json.indexOf(imagesInUploadsFolder[i]) > -1) {
        console.log('In database: ' + imagesInUploadsFolder[i]);
    } else {
        console.log('Not in database: ' + imagesInUploadsFolder[i]);
    }
}
for (var i = 0; i < imagesInUploadsFolder.length; i++) {
    console.log(imagesInUploadsFolder[i] + ' in ' + imagesInDatabaseTable);
    if (imagesInDatabaseTable.indexOf(imagesInUploadsFolder[i]) > -1) {
        console.log('In database: ' + imagesInUploadsFolder[i]);
    } else {
        console.log('Not in database: ' + imagesInUploadsFolder[i]);
    }
}
Comparing two arrays in javascript and creating new arrays with missing and new elements

Comparing two arrays in javascript and creating new arrays with missing and new elements


By : kamikaisan
Date : March 29 2020, 07:55 AM
Does that help What is the most efficient way to compare two javascript arrays and create two new arrays of missing and new elements? Array elements will be always strings or numbers and it is not 100% sure that they will be sorted in any way. , The code to Drew Noakes' solution:
code :
var old_array = ['11', '13', '14', '18', '22', '23', '25'];
var new_array = ['11', '13', '15', '16', '17', '23', '25', '31'];
var missing_elements = _.difference(old_array, new_array);
var new_elements = _.difference(new_array, old_array);

console.log(missing_elements); // would produce ['14', '18', '22']
console.log(new_elements); // would produce ['15', '16', '17', '31']
comparing two javascript arrays?

comparing two javascript arrays?


By : Hasan Basri
Date : March 29 2020, 07:55 AM
To fix the issue you can do I want to compare two arrays with each other and see if there is a match, and if there is do something. , Try using this:
code :
for(var i = 0; i < answers.length; i++) {
    for(var j = 0; j < correct.length; j++){
        if (answers[i] === correct[j]){ 
            console.log(answers[i]+ " is the correct answer")
            break;
        }
    }
}
Comparing arrays in javascript error

Comparing arrays in javascript error


By : Daniel Antunes
Date : March 29 2020, 07:55 AM
it fixes the issue There are several things to correct. Mainly you could use JSON.parse() to extract the data:
code :
var files= '{"files":[{"name":"doc1.pdf","title":"networking","path":"mfpreader.comze.com\/files\/doc1.pdf"},{"name":"doc2.pdf","title":"Armoogum","path":"mfpreader.comze.com\/files\/doc2.pdf"}]}';
var result = '[{"name":"doc1.pdf","title":"networking","path":"mfpreader.comze.com\/files\/doc1.pdf"}]';

// you need to convert the above strings to arrays:
files = JSON.parse(files).files; // You need the files property
arrayResults = JSON.parse(result);

for (var i = 0; i < files.length; i++) {
    var file = files[i];
    for(var j=0;j<arrayResults.length;j++){
        // note the name property on the files[i] object:
        if (files[i].name===arrayResults[j].name){ 
            alert("Matching found for " + files[i].name);
        }else {
            alert("no similar files for " + files[i].name);
        }
    }  
}
Comparing two arrays in Javascript

Comparing two arrays in Javascript


By : Hacker
Date : March 29 2020, 07:55 AM
I wish this help you Use a higher-order function that accepts an array (which changes with each iteration of y) and returns a new function that operates on each element (nested array) in some. It returns true if the arrays contain the same elements regardless of order.
code :
function matches(outer) {
  return function (el) {
    if (outer.length !== el.length) return false;
    return el.every(function (x) {
      return outer.indexOf(x) > -1;
    });
  }
}
function finder(x, y) {
  return y.filter(function (el) {
    return !x.some(matches(el));
  });
}

finder(x, y);
Related Posts Related Posts :
  • clearvalidators not working in angular 6 form control
  • Remove Count Attribute in json data
  • queue up function callbacks
  • How to stop all Soundcloud streams in once?
  • Sliding div onclick left/right
  • RequireJS JQuery shim doesnt work with VS2013
  • What is the Meteor concurrency model?
  • Events on children of contenteditable element
  • Simple jquery not working to print the input value
  • Javascript templating language in reverse
  • Making Pagination in Javascript Scalable (for large # of db results)
  • Performance of Stateless Authentication in SPA and SSO (Single Sign On)
  • Assigning a value for view asynchronously
  • Mocha JS: How to reuse assertions within a spec?
  • Is ember.js stable enough to use in a real world project?
  • jQuery Load() not loading within div
  • encode stream from webCam h264
  • Regex to find pattern, return subpattern
  • Why does this regex to remove empty <p></p> tags from a string fail?
  • Rails: pass data to javascript
  • Quickest way to check if a number is in a set?
  • Phonegap events online/offline not working
  • In HTML5, please explain the meaning of '?' in the statement
  • Form to use AJAX rather than a page refresh but also utilise error handling?
  • JQuery Script looping error
  • different size divs align top left
  • Image fade-in when webpage finishes loading
  • Seeking HTML5 audio element causes delay (breaks sync)
  • How can I read & write data to a new tab in Firefox?
  • jQuery or JS turn all words into italic type
  • iOS css -webkit-transform: scale doesn't offset touch events
  • Populate select list using javascript
  • Can you work out why my .js file doesn't seem to be linking to my html?
  • Make slideToggle effect only one element at a time
  • Output sound using JavaScript
  • What is the analytics.js equivalent of _trackPageview found in ga.js
  • How can I check if a JS file has been included already?
  • hasClass and condition not working
  • EDITED: I'm having issues with a button and its function
  • Importing XML in HTML with javascript
  • Create new function in contentScriptFile in Firefox extension
  • Should I build Node.js from source or download the OS X installer?
  • How to enforce integer sizes in JS? (TypeScript?)
  • How to update the JSON object without deleting old values using javascript
  • Reading a javascript variable containing a string into another file
  • Possible to prevent 403 errors from logging to the web console?
  • Jquery Accordion - Expand and collapse div on hover
  • jquery .hide() function - Hide mobile, Display Desktop
  • Holding password in javascript variable - security implications?
  • JQPlot trouble to render vertical stacked bar and horizontal legend
  • counts days inbetween then add class to those days with nested divs?
  • Marionette.js with Rails (Devise) Authentication
  • javascript anonymous function evaluation idiom
  • Why am I losing the decimal when working this equation in Javascript?
  • Site redirects to index.php after being open while making calls to an API using AJAX
  • How to determine when multiple callbacks are all done
  • How to update Yeoman dependencies?
  • How can I do a /resolve with the Soundcloud Javascript SDK?
  • I'm not able to call a named callback function
  • How to get value of Paragraph element contained in <div> in HTML using JavaScript
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org