Javascript Array remove(remover)

Description

Javascript Array remove(remover)


/**/*from  w w w.  j a v  a 2  s  . c o m*/
 * Remove an element from an array
 * @param remover {*} - the element to by removed
 * @returns {Array} - a new filtered array
 */
Array.prototype.remove = function remove(remover) {
  return this.filter((element) => {
    if (typeof element === 'object') {
      let property;
      for (property in remover) {
        if (remover.hasOwnProperty(property)) {
          return element[property] !== remover[property];
        }
      }
    }
    if (Array.isArray(element)) {
      return JSON.stringify(element) !== JSON.stringify(remover);
    }

    return element !== remover;
  });
};

export default Array.prototype.remove;

Javascript Array remove(remover)

/**//www.j a v a2  s .  c o m
 * Remove an element from an array
 * @param remover {*} - the element to by removed
 * @returns {Array} - a new filtered array
 */
Array.prototype.remove = function remove(remover) {
    return this.filter((element) => {
        if (element.constructor === Object) {
            for (const property in remover) {
                if ({}.hasOwnProperty.call(remover, property)) {
                    return element[property] !== remover[property];
                }
            }
        }
        if (element.constructor === Array) {
            return JSON.stringify(element) !== JSON.stringify(remover);
        }

        return element !== remover;
    });
};

export default Array.prototype.remove;



PreviousNext

Related