Should.js: check if two arrays contain same strings

should js compare objects
sinon should
should.js npm
should be truthy
should.js typescript
should js github

I have two arrays:

var a = ['a', 'as', 'sa'];
var b = ['sa', 'a', 'as'];

Is there anything special in shouldJS to test if these two arrays have same items? Anything Like

should(a).be.xyz(b)

that can test them? Here, xyz is what I am looking for.

A naive, but possibly sufficient solution would be to sort the arrays before comparing them:

should(a.sort()).be.eql(b.sort())

Note that sort() works in-place, mutating the original arrays.

Should.js API Documentation, This getter will unwrap all standard wrappers like Number , Boolean , String . It should accept 2 arguments: should function and Assertion constructor Only modifier - currently used with .keys to check if object contains only exactly The same like Assertion#containDeepOrdered but all checks on arrays without order. Check if two unsorted arrays (with duplicates allowed) have same elements Given two unsorted arrays, check whether both arrays have same set of elements or not. Examples:

You could implement this with should's Assertion.add feature. For example:

var a = ['a', 'as', 'sa'];
var b = ['sa', 'a', 'as'];

should.Assertion.add('haveSameItems', function(other) {
  this.params = { operator: 'to be have same items' };

  this.obj.forEach(item => {
    //both arrays should at least contain the same items
    other.should.containEql(item);
  });
  // both arrays need to have the same number of items
  this.obj.length.should.be.equal(other.length);
});

//passes
a.should.haveSameItems(b);

b.push('d');

// now it fails
a.should.haveSameItems(b);

Should.js API documentation, It contains well written, well thought and well explained computer science and length of both arrays should be same, the objects present in it are of the same type and each By doing this we can conclude both arrays are the same or not. By converting into JSON string we can directly check if the strings are equal or not. How to find if two arrays contain any common item in Javascript? Given two arrays containing array elements and the task is to check if two arrays contain any common elements then it returns True otherwise return False.

Slightly improved version of Michael's code:

should.Assertion.add("haveSameItems", function (other) {
    this.params = { operator: "to be have same items" };

    const a = this.obj.slice(0);
    const b = other.slice(0);

    function deepSort(objA, objB) {
        const a = JSON.stringify(objA);
        const b = JSON.stringify(objB);

        return (a < b ? -1 : (a > b ? 1 : 0));
    }

    a.sort(deepSort);
    b.sort(deepSort);

    a.should.be.deepEqual(b);
});

How to compare two arrays in JavaScript?, string vs. number vs. function). That they have the same value. And if the item is itself an array or object, we need  Check if two arrays are equal or not. Given two given arrays of equal length, the task is to find if given arrays are equal or not. Two arrays are said to be equal if both of them contain same set of elements, arrangements (or permutation) of elements may be different though. Note : If there are repetitions, then counts of repeated elements must also be same for two array to be equal.

Check if two arrays or objects are equal with JavaScript, BDD style assertions for node.js -- test framework agnostic. you should have browserify $ npm install -g browserify $ make browser even if it is different types, but actual content the same [1, 2, 3].should.eql({ '0': 1, '1': 2, '2': 3 }); Array​, .Boolean, .Function, .String, .Error. Assert given object is instance of the given  Assert that given object is contain equally structured object on the same depth level. If given object is an array and other is an array it checks that the eql elements is going in the same sequence in given array (recursive) If given object is an object it checks that the same keys contain deep equal values (recursive)

Should.js, Two arrays are said to be equal if they have same elements in the same order. The solution should work for nested arrays of any depth. arrays into a JSON string and compare their string representation with each other to determine equality. Approach: We have two strings now we have to check whether the strings contain the same characters in the same order. So we will replace the contiguous similar element with a single element i.e. if we have “eee”, we will replace it with a single “e”. Now we will check that both the strings are equal or not. If equal then print Yes else No.

Compare Arrays in JavaScript, Here's a Code Recipe to check if a #JavaScript array contains a value. You can Besides strings, includes also works great with other primitive types . So far, I have shown you values where both includes and indexOf work interchangeably. Bitwise XOR of a Binary array; Program to check if a String in Java contains only whitespaces; Check if given Parentheses expression is balanced or not; Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings; Program to check if the String is Null in Java; Check if a word is present in a sentence; Sort an

Comments
  • This answer is not correct. .equal uses === for reference equality. Need to use .eql.
  • @denbardadym Thanks for the note, updated my answer.