Having trouble getting Lodash filter to work with multiple conditions?

Are you facing issues with using the lodash library to filter an array of objects? If the filter is returning the same value that was passed into it, there might be something wrong in your implementation. Here's the function I am using to transform data:

getFlattenFields(schema)
    {
        let flatten = _.flatten(schema.fields);
        console.log('flatten',flatten);
        let filtered = _.filter(flatten, item => item.element != 'loader' || item.element != 'button' );
        console.log('filtered',filtered);
        return filtered;
    },

The `schema` variable being used is as follows:

formSchema: 
    {
        fields: 
        [
            [ 
                { id: 'email', label: '', default: '', element:'input', type: 'text' },
                { element: 'button', text:'Enviar', icon:'fas fa-reply' }, 
            ],
            [
                { element: 'loader' }, 
            ]
        ]
    }

Answer №1

Your issue lies in the condition you are using:

item => item.element != 'loader' || item.element != 'button'

This condition will always evaluate to true because every element cannot be equal to both loader and button at the same time. It seems like you may actually want:

!(item.element === 'loader' || item.element === 'button')
.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mastering the art of manipulating arrays with jquery

Trying to implement a dynamic search bar feature using jQuery. Here is the code snippet: Fiddle : https://jsfiddle.net/fpLf1to4/ var inputSearch = $('.searchInput').hide(); var searchArray = ['s','e','a',& ...

What should be done when HTML5 anchor tag downloads fail due to a long base64 string in the src attribute?

batchDownloadImages() { const aTagDownload = [ { download:'foo', href:'HD image base64 string from canvas.toDataUrl()' }, { download:'bar', href:'HD image base64 string from canvas.to ...

What is the best way to extract formatted data from objects using a function?

Below is an example of an object structure: const appUrls = { 'subject': 'Math', 'questions': { 'primary': 12, 'secondary': 13 } 'marks': '100', 'students': ...

The slider components have an endless loop feature that only runs once before coming to a

I'm working on creating an infinite slider that loops continuously without the need for navigation buttons. Instead, I plan to allow users to control the slider using touch gestures (although this functionality is not yet implemented in the code snipp ...

Display the JQuery element that contains a child element with a specified class

On my webpage, I have a dropdown menu (ul) and I want to keep it open when the user is on the page. Specifically, I only want to display the ul item if it contains an li element with the class ".current-menu-item". The code snippet below accomplishes this ...

I have successfully implemented a click effect on one image and now I wish to apply the same effect to the remaining images

I've exhausted all my options and still can't crack this puzzle. Let me break it down for you: Upon entering the page, users are greeted with a collection of images each tagged with classes (for example, img01 and img02). When an image is clicke ...

The annotation feature in Highcharter is reportedly malfunctioning when applied to the date x-axis in

I've been attempting to include annotations in my highcharter chart, but for some reason, it's not working and is displaying [object][object] instead. Below is the data: structure(list(variable = structure(c(15522, 15553, 15584, 15614, 15645, ...

Sliding an image from the left side to the right, and then repeating the movement from left to right

I am currently working on a CSS effect for some images. My goal is to have an image appear from the left side, move towards the right side, then reappear from the left and repeat this cycle. I managed to achieve this using the code below: @keyframes sli ...

The target for ajaxSubmit is being duplicated instead of being replaced

I encountered a problem with the code below: $('#refresh').click(function () { alert($('.report-container').length); $('.report-container').each(function () { var accordian = this; var url = $(this) ...

Searching in mongoose will not yield any results if the specified field does not exist in the schema

My mongoose Schema is structured as follows: const mongoose = require('mongoose'); const Schema = mongoose.Schema; const CitizenSchema = new Schema({ SSN: { type: Number, required: true, unique: true, }, firstName: { type: ...

Display numerical values ranging from a to b based on a specific requirement

Seeking guidance as a beginner in JavaScript. I need help with finding the best approach to solve this problem: I have two integers, a and b, where a < b. I want to print all integers in the range [a;b] inclusive. The pattern should be such that integ ...

The split function is creating redundant values

Please help me with this issue I am facing while trying to split a string using split(). It's causing duplicate values which is frustrating. Can someone please assist me? The string I need to split is a comma-separated response from a database, and h ...

Tips for leveraging JavaScript functions repeatedly

Need help with using the Mul() function in a second table row. I am struggling to call it more than once while running my code that requires creating and calling a function multiple times. Can someone please assist me? Thank you. function Mul() { var ...

Encountering a null value in a function parameter assigned within an AJAX success callback in JavaScript

function fetchData(url) { var response = null; $.ajax({ type: "GET", url: url, crossDomain: true, async: false, contentType: "application/json; charset=utf-8", da ...

Handling image errors in jQuery for responsive design queries

Images on my website change based on the screen size using media queries. I am looking for a way to handle errors that may occur using jQuery. I want to display a text message if the image fails to load. Here is the code I have so far: <div class="con ...

How can JavaScript automatically calculate the sum of various numbers entered into multiple input fields?

I would like to automatically add different numbers entered in multiple input fields using JavaScript. Is there a way to calculate the sum of numbers entered in various input fields using JavaScript? "The input field consists of n numbers..." and I am ut ...

Modifying CSS content attribute in real-time

A colleague of mine has rented a webshop from a company. They have the option to select different templates and are also able to customize the CSS and add Javascript snippets. They reached out to me for help with making some modifications, but there's ...

Issue with Mongoose $in operator causing updates to only affect a single document

Having some issues with the "$in" operator in Mongoose. I have a User schema that includes an array of Card schema. The Card schema contains a 'score' field that I want to update based on a list of Card ids. Here's what I have attempted: Us ...

The function of jQuery .click() not triggering on elements within msDropDown

I'm having difficulty implementing jQuery on an Adobe Business Catalyst site. The HTML snippet below shows the structure: <div class="banner-main"> <div class="banner-top"> <section class="banner"> <div class="catProd ...

issue with transferring a function from a parent component to a child component

While attempting to pass a function from the parent component to the child component, I encountered an issue that I am struggling to resolve. Every time I try to pass props to the child component, it doesn't seem to work and gives me the following err ...