selector for 'any of list' in LINQ.JS

Is there a LINQ.JS equivalent to the SQL "in" operator that I can use with the following array:

var fruits = [
    {name:"apple" , color:"red" }  
    {name:"banana" , color:"yellow" } 
    {name:"cucumber" , color:"green" }  
    {name:"cherry" , color:"red" }  
    {name:"strawberry" , color:"red" }  
    {name:"orange" , color:"orange" }  
    {name:"lemon" , color:"yellow" }  
    {name:"tangerin" , color:"orange" }  
    {name:"grape" , color:"purple" }
]

I am looking for a LINQ.JS query that will retrieve all red and yellow fruits. I need something similar to this SQL command:

select * from fruits where color in ('red','yellow')

Answer №1

let items = [
    {name:"shirt"       , color:"blue" },  
    {name:"pants"      , color:"black" }, 
    {name:"hat"    , color:"green" },  
    {name:"socks"      , color:"white" },  
    {name:"shoes"  , color:"red" }  
];

let searchValues = ["blue", "red"];
let fieldName = "color"

console.log(items.filter(item => searchValues.some(value => value === item[fieldName])));

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

Tips for optimizing AJAX content for Google indexing

As I begin the process of developing a public website that utilizes client-side rendering with AngularJS, I have come across information suggesting that dynamically generated content may not be properly indexed by Google. This raises concerns about the imp ...

Preventing browsers from sharing sessions across tabs: Tips and tricks

Is there a way to prevent session sharing between multiple browser tabs? In my JSP/Servlet application using Spring Security, I am interested in achieving the behavior where users are prompted to log in again whenever they switch browser tabs. Please not ...

Enhancing the Look of Custom HTML Tags with React JSS Styling

I have incorporated material-ui into my project, here is an example of the code I am using: import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import Button from '@material-ui/core/Button'; cons ...

Tips for efficiently searching and filtering through an array of objects when using setState

Currently, I am working on developing a search feature using an array of objects with react. The data is structured as follows: const data = [ {"category 1" : [ { "name": "Orange", "desc": "juice, orange, Water" ...

Using Vue.js: Execute a function with a delay, but start the delay over if there is any user input

Imagine a scenario where I have a form that is connected to an API and displays information based on user input. Whenever the user makes changes, such as adjusting the number of results per page, the component should react dynamically by loading new data. ...

Explain the functionality of square brackets in the C programming language

As a beginner in C programming, I am eager to grasp the fundamentals. Tutorials often lack in-depth explanations, leaving me seeking answers that are easily understandable. Consider the following code snippet: #include <stdio.h> #include <stdlib ...

Update the input value following a successful action

How can I update the value of an input field after a successful ajax call? I have tried the following approach but it doesn't seem to be working. The result from the alert is 80000 Here is the HTML input code: <input class="form-control" type=" ...

Using the attribute to pass an object to a directive and including a variable inside

Is it possible to transfer $scope.data to a directive through its attribute in an Object, without using separate attributes? I would like to achieve the below format with any solution. <custom-directive detail="{index:1, data: {{data}}}"> </custo ...

Error message "jQuery ajax call receiving 405 (POST method not permitted) within Laravel application"

I'm really struggling with this issue. Could it be related to an Apache configuration? I've come across some filters.php configurations for adding POST, but if that were the problem, wouldn't it be documented in Laravel's official docs? ...

Leveraging the power of GSON for parsing a JSON array

Looking at the structure of my JSON file, it appears like this: [ { "number": "3", "title": "hello_world", }, { "number": "2", "title": "hello_world", } ] In the past, when files had a root element, I used to u ...

Is there a way for me to retrieve a function from a different JavaScript file using jQuery?

Within the HTML, there exists a script referred to as Normal.js: $(function () { function WhiteNav() { $("#LOGO>img").attr('src', '/images/LOGOWhite.svg'); $("#NavUL a").css("color", "#cecece"); } }); ...

Ways to change babel transpiled code back into regular JavaScript

I have been utilizing a plugin known as babel-plugin-transform-react-class-to-function to convert React classes into functional components. While the plugin successfully transpiles the input code, I actually need the original plain code as outlined in the ...

How can I display several custom markers that are constantly updating on a Google map with MySQL and PHP?

Currently, I am using the following code to generate markers on a Google map by retrieving data from a database. However, the issue I am facing is that it only generates one marker instead of all the markers stored in the database. & ...

Click on the button to automatically scroll to a specific component inside the dialog

In my JSF/Primefaces application, I have a page with a long content that can be scrolled, along with a dialog also containing lengthy content that requires scrolling. The dialog includes a button and I am looking to scroll the dialog content to a specific ...

The jQuery Shapeshift extension. Components that do not align in a row

Having an issue with the jQuery Shapeshift plugin. Trying to arrange blue, green, and yellow blocks in a single row, but Shapeshift is not cooperating. Any ideas on what could be causing this and how to fix it? Here's the HTML: <div class="contai ...

Media publications do not conform to the current trends

I'm currently utilizing the HTML-to-paper plugin to print my content on a printer. However, I've encountered an issue where it doesn't seem to apply any of the styles I've defined within @media print. The challenges I'm encounteri ...

"Enhancing the Today Button in FullCalendar with Custom Functionality

My issue lies with the implementation of fullCalendar. Specifically, I am utilizing the week view (defaultView: 'basicWeek') with the toolbar buttons for 'today', 'prev', and 'next'. The problem arises when I click t ...

Exploring the depths of data: R and SQL Server integration

I wrote this SQL code: *enter code here* SELECT * FROM database1 WHERE (Name LIKE '%T%') AND (City NOT LIKE '%Tokyo') The above SQL query works perfectly in SSMS. Now, I am working with R Studio and trying to get user input from a t ...

The ::before pseudo element is malfunctioning when used in the makeStyles function

I am currently utilizing the makeStyles function in React, but I seem to be facing an issue where the content within the ::before pseudo-element is not displaying. Strangely enough, when the content is an image it works fine, but text does not render. Jus ...

Is it considered a secure practice to compare a user's IP address with the logged-in session's

I have created a JavaScript game using the HTML5 canvas tag that communicates with a server through AJAX to update the database. The issue I am facing is that users are able to manipulate data sent to the server, potentially leading to unauthorized actions ...