score:6
Accepted answer
Your second array is not valid. You have to wrap the string values with quotes.
You can use Array.prototype.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
and Array.prototype.includes()
The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
Try the following way:
var arr1 = [1, 3, 4,];
var arr2 = [
{
id: 1,
title: 'Title 1',
},
{
id: 2,
title: 'Title 2',
},
];
var res = arr2.filter(i => arr1.includes(i.id));
console.log(res);
Source: stackoverflow.com
Related Query
- Loop through array of objects, check for a matching parameter and add the matching object to new array
- How to recursively loop through array of objects and find the index of matching id using javascript and react?
- Loop through the array and return a `Dropdown.Item` element for each item
- trying to loop through the array of objects and find the match
- Looping through array of objects and return the key and value for each object
- In Javascript, replace forEach that involves filtering the same array of objects each time through loop with a left join
- How I can loop through the similar objects in state and push them into object?
- In React.js, how can I loop through an array of jsx elements and add attributes to them
- How to loop through an array of strings backwards x amount of times and start over when the end is reached?
- Loop through Array of object with the same data on some object and get it displayed once
- In React, when using map() to loop through an array of elements, does the result need to only be displayed using the <ul> and <li> tags?
- Whats the difference between Array.fill and a for loop to create Array
- How to group arrays of objects by value and add the result to another array with same value at specific key
- What is the correct approach in React to loop through an array to build html-select list of items in a function and call it in render()?
- How can you check if there is an existing value in the array and not add it another time if so?
- react loop through array of strings with delay and set the text value
- Loop through array of objects and return object keys
- Loop through an array of objects, find duplicate objects by id, add their values in a single object, react js
- How to loop through an array an put create an array of objects using react and javascript?
- Loop through array of objects and display them in react component
- How to loop through an array with keys and values in to send a request for Axios.get's params in ReactJS?
- How can I compare two arrays of objects and add new key to an objects in the second array
- How do I iterate over an array of objects match a common element from another array and return the key value for "name"
- Chosing first two from the array and the add +(plus) for rest of them
- I want to loop in an array in the state and check if the input is exist in it
- Get the component from array of objects and add new properties to it - React
- Iterate through a for loop with the help of react and read out the data record
- How to loop array and output the values inside an array of objects as 'value' and 'label'?
- Loop through two array and based on condition change the property value not working
- How to map through an array of objects and add value to each other in React?
More Query from same tag
- How do I get data from Axios and return it from a Function Component in React?
- deploying officejs fabric react word add in
- returning 3 arrays from a react custom hook
- Tree Table - Setting children on react.js
- Formik checkbox not showing tick when added in a map function
- React, Graphql & Passport.js
- React TS shared styled component props
- How do I define a TypeScript type for React props where prop B is only accepted if prop A is passed to a component?
- React material - getting TypeError: Cannot set property 'transition' of undefined when testing popper component
- How to iterate over different variables/states to create an object?
- React/Redux export default connect() doesn't seem to be connected to Provider
- why i am getting this error on using Link tag from next/link? Error: React.Children.only expected to receive a single React element child
- get value when updated,outside of formik using useRef()
- Can't find variable: regeneratorRuntime (ReactNative)
- Acessing refs with render props
- Turn String to JSX - curly brackets issue
- React: Accept generic interface as prop
- Multiple events to create a dynamic +/- editable input
- Initial state in React with Redux not working
- Storybook: auto generate docs from typescript
- redirect to new page after login in react
- How to give different backgrounds to the same component?
- Module not found: Error: Can't find options with ident 'postcss'
- React useToggle component returning TypeError: Object is not a function or its return value is not iterable
- Get dropdown title to change to selected dropdown value
- Updating states from input field when using React-Redux
- Textarea add div for each space between each word
- How to make a semi circle chart in react?
- How to update properties of an array nested inside objects in ReactJS
- How to get updated state immediately with redux?