skip to Main Content

How can I achieve this kind of conditions in javascript? (via: vanilla or lodash)

const array1 = [1, 2]
const array2 = [1, 2, 3, 4] //true
const array2 = [1, 3, 2, 4] //true
const array2 = [4, 3, 2, 1] //false
const array2 = [2, 3, 1, 4] //false

I tried something like this.

const _ = require('lodash');

const array1 = [1, 2];
const array2 = [1, 2, 3, 4];

const trimmed = array2.filter((n) => combinations.indexOf(n) > -1);
const isEqual = _.isEqualWith(array1, trimmed);
console.log(isEqual);

Please post if you have much cleaner and much flexible code for this problem.

2

Answers


  1. Use Array.every() to check if each item in arr2 is either not included in arr1 or is found in place i (the current index of arr1):

    const fn = (arr1, arr2) => {
      let i = 0
      
      return arr2.every(v => !arr1.includes(v) || v === arr1[i++])
    }
    
    console.log(fn([1, 2], [1, 2, 3, 4])) // true
    console.log(fn([1, 2], [1, 3, 2, 4])) // true
    console.log(fn([1, 2], [4, 3, 2, 1])) // false
    console.log(fn([1, 2], [2, 3, 1, 4])) // false

    A variation on this would be to shallow clone the 1st array using destructuring a rest syntax, and then use Array.shift() to always check the current 1st item of arr1:

    const fn = ([...arr1], arr2) => arr2.every(v => !arr1.includes(v) || v === arr1.shift())
    
    console.log(fn([1, 2], [1, 2, 3, 4])) // true
    console.log(fn([1, 2], [1, 3, 2, 4])) // true
    console.log(fn([1, 2], [4, 3, 2, 1])) // false
    console.log(fn([1, 2], [2, 3, 1, 4])) // false

    With lodash – get the intersection of arr2 and arr1 (same items, regardless of order), and then check if it’s equal to arr1 (same items, same order):

    const fn = (arr1, arr2) => _.isEqual(
      _.intersection(arr2, arr1),
      arr1
    )
    
    console.log(fn([1, 2], [1, 2, 3, 4])) // true
    console.log(fn([1, 2], [1, 3, 2, 4])) // true
    console.log(fn([1, 2], [4, 3, 2, 1])) // false
    console.log(fn([1, 2], [2, 3, 1, 4])) // false
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    Login or Signup to reply.
  2. Considering all elements in array1 will be present in array2, you apply two pointer approach.

    Something like this:

    /** Checks if arr2 is partially ordered as arr1 */
    function isOrdered(arr1, arr2) {
      let i = 0;
      for (let j = 0; j < arr2.length && i < arr1.length; j++) {
        if (arr1[i] === arr2[j]) i++;
      }
    
      return i === arr1.length;
    }
    
    console.log(isOrdered([1, 2], [1, 2, 3, 4])); // true
    console.log(isOrdered([1, 2], [1, 3, 2, 4])); // true
    console.log(isOrdered([1, 2], [4, 3, 2, 1])); // false
    console.log(isOrdered([1, 2], [4, 3, 2, 1])); // false
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search