I am looking to filter an array to only include products with IDs that match those in the first array

I'm attempting to filter an array to only include products with IDs found in another array. Here's what I've tried so far:

let first = [1, 4]
let second = [{id: 1}, {id: 2}, {id: 4}]
second.filter((es, i) => es.id.includes(first))

https://i.sstatic.net/W5N12.png

Answer №1

To ensure the correct functionality, it is essential to invert the include condition so that it reads first.includes(es.id) instead of es.id.includes(first)

let first = [1, 4]
let second = [{id: 1}, {id: 2}, {id: 4}]
let result=second.filter(es => first.includes(es.id))
console.log(result);

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

Next.js encountered an issue when trying to read properties of null, specifically the 'push' property, resulting in a TypeError

I am utilizing the sweetalert2 library for displaying popups: export default function Home() { const MySwal = withReactContent(Swal) useEffect(() => { MySwal.fire({ showConfirmButton: false, customClass: { ...

Transform Array into an unordered list with list items

Currently, I am dealing with a file that contains strings of file paths in the following format: ./CatDV/S1/SFX/steam/6004_90_04 LargeHeadlightSm.wav ./CatDV/S1/SFX/steam/AirHissPressureRe HIT032001.wav ./CatDV/S1/SFX/steam/Impact_Metal_Bullet_Hit(1).wav ...

Using Vue.js 3 composition, I have encountered an issue where the v-if directive is not functioning as expected on two separate div

Is there a way to seamlessly toggle sections of the app in Vue.js 3 composition using v-if? I managed to make it work with the vue.js 2 API option initially, but since transitioning to vue.js 3 API Composition, the section isn't hiding despite screenM ...

What is the best way to integrate properties subsets into your classes?

In my code, I am working with 3 classes ... class1 { constructor(a, b, c) { this.a = a; this.b = b; this.c = c; this.toClass2 = function() { // TODO: return this as an instance of class2; // the conversion would remove the un ...

Making alterations to the content of a division using getElementById().innerHTML yields no difference

Recently, I've been working on a JS project inspired by a short story from my high school days. The story featured robot crabs living on a small island, and I wanted to create a simulator where players could set the initial parameters and watch as the ...

Steps for adding a variable to a JSON request

I'm new to JavaScript and React Native and I'm looking for a way to include variables in a JSON request. Specifically, when making a JSON request using Fetch or Axios to a URL, I need to customize the URL based on certain parameters. For instance ...

Playing the Jump Game using recursion

I am challenging myself to master recursion by attempting the Jump Game exercise. Given an array of non-negative integers, your goal is to reach the last index in the minimum number of jumps. While working on this part of the code, I encountered a problem ...

Creating a see-through effect in Three.js with React Fiber

I'm currently working with react fiber and struggling to make the background of my child scene transparent. Below is my root component containing the main Canvas element: export const Splash = () => { const { intensity, distance, colo ...

Tips on leveraging dynamic queries in your REST API usage

As a new Javascript learner, I am experimenting with creating a basic rest api using node.js. Currently, I have set up a database called testDb and a table named testMeasurement in influxdb. The testMeasurement table consists of the columns DateOfBirth, ID ...

What is the best way to verify a set of requests concurrently before proceeding with their execution or flagging an error?

Below is the code I have written for liking a post on my blog website, which involves three phases. The first phase is adding the liked post to the user's list of liked posts, the second phase is adding the like to the post itself, and the third phase ...

Update the CSS for InputLabel

I have a drop-down list that I want to customize. The issue is illustrated below: https://i.sstatic.net/hzVtl.png I'm looking to center the text "choose format" within the field and adjust the font size. return ( <FormControl sx={{ m: 1 ...

Defining the return type with TypeScript using the keyof accessor: A comprehensive guide

Can a function be created that utilizes keyof to access an object's property, with TypeScript inferring the return value? interface Example { name: string; handles: number; } function get(n: keyof Example) { const x: Example = {name: &apo ...

Efficiently transferring components of a JavaScript project between files

For the first time, I am creating an npm package using ES6 and Babel. However, I am facing difficulties in connecting everything together so that it can be imported correctly by the end user. The structure of my build (output) folder is identical to src: ...

What is the location where nvm saves its node.js installations?

I'm having trouble locating where node.js installations are stored after downloading and installing through commands like: nvm install 5.0 Can anyone provide some insight on this? ...

Can someone help me figure out how to add a new key value pair to an array in PHP?

Despite the abundance of documentation, I struggled to locate this one line of code in a 4000-line file. Accuracy is key on the first attempt. file_put_contents($myFile,serialize(array($email_number,$email_address))) or die("can't open file"); if ...

Tips for comparing two arrays of test dsa das dsge a:

My task is to compare and evaluate two arrays to determine if they both have the property 'approved' set to true. ...

When attempting to access /test.html, Node.js server returns a "Cannot GET"

Embarking on the journey of diving into Pro AngularJS, I have reached a point where setting up the development environment is crucial. This involves creating an 'angularjs' directory and placing a 'test.html' file in it. Additionally, o ...

What methods can be used to test included content in Angular?

When testing an Angular component that includes transclusion slots utilizing <ng-content>, it becomes challenging to verify if the transcluded content is correctly placed within the component. For instance: // base-button.component.ts @Component({ ...

What is the best way to retrieve multiple variables using Express.js on Node.js?

Trying to fetch Form data from an HTML page to a Node Js server. The HTML file is named index.html <body> <nav> <ul> <li> <a href="#" class="button add">Add Product</a> <div class="dialog" style="displ ...

Ways to avoid losing data when a page is refreshed

After encountering a frustrating issue with my form, I turned to research in hopes of finding a solution. However, each attempt has resulted in disappointment as the data is lost every time the page is refreshed. If anyone has advice on how to prevent th ...