Combine two arrays of objects and merge properties using the Ramda library

I have two arrays as shown below:

['TAG.u', 'TAG.c'] 

and the other one is:

[{name:'some',key:'TAG.u'},
{name:'some new', key: 'TAG.b'}, 
{name:'some another' , key:'TAG.c'},
{name: 'some big' , key:'TAG.a'}]

Now I want to combine these two arrays into a new array with a new property selected: true for each matching key and false for others, like this:

result: [{name:'some', key: 'TAG.U', selected: true} ,
{name:'some another' , key:'TAG.c' , selcted: true},
{name:'some new', key: 'TAG.b', selected: false},
{name: 'some big' , key:'TAG.a' , selected: false} ]

Thank you for any assistance.

Answer №1

Here is how I would approach it:

const merge = curry((words, content) => 
  map(c => assoc('included', contains(c.key, words), c), content)
)

merge(words, content)

While we could potentially refactor this to be point-free, I personally find the current structure clear and concise.

To see this function in action, you can check out the Sample REPL.

Answer №2

If you want to check if the key values of each object in the second array exist in the first array, you can utilize array.map().

var arr1 = ['TAG.u', 'TAG.c'];
var arr2 = [{
    name: 'some',
    key: 'TAG.u'
  },
  {
    name: 'some new',
    key: 'TAG.b'
  },
  {
    name: 'some another',
    key: 'TAG.c'
  },
  {
    name: 'some big',
    key: 'TAG.a'
  }
];

var result = arr2.map(v => {
  v.selected = arr1.indexOf(v.key) > -1;
  return v;
});

console.log(result);

Answer №3

To merge two lists using a specific function, you can leverage the power of ramda's zipWith

R.zipWith(
  (item, tag) => R.assoc('selected', item.key === tag, item),
  items,
  tags
 )

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

The Shopify JSON array is displaying some results, however, there are also empty results being

I'm trying to retrieve only available results, but the json response is showing null,null,null,result, result. Below is the code snippet: {% capture results %} {% for item in search.results %} {% assign product = item %} {% if product.avail ...

"Mastering the Art of Placing the VuetifyJS Popover: A Comprehensive

When using VueJS with VuetifyJS material design components, how can I adjust the positioning of the Vuetify popover component to appear below the MORE button? The current placement is in the upper left corner which I believe defaults to x=0, y=0. Button: ...

Catching the Selenium NoSuchElementError in JavaScript is impossible

Update: It's puzzling why this was marked as answered since the linked questions don't address the issue at hand and do not involve Javascript. My objective is to detect this error, rather than prevent it, given that methods like invisibilityOfEl ...

Error occurs when attempting to reference an object from an NPM package

Currently, I'm attempting to utilize the https://github.com/iamcal/js-emoji library for colon-to-emoji conversion. Following the installation of its NPM package, I included <script src="../node_modules/emoji-js/lib/emoji.js" type="te ...

What is the process for changing the state of an array in graphql?

Currently, I am facing a challenge with adding an array to a GraphQl schema. Here is what it looks like: type Play { winRatio: [Float] } The intention behind the winRatio attribute is to store the history of all win ratios. For instance, after playing 3 ...

Exploring ways to retrieve item metadata from a Stripe checkout session

When setting up a Checkout session, I dynamically create prices using the price_data and product_data properties. I include metadata for each item within the product_data.metadata property. However, after a successful payment is made, I retrieve the sessi ...

What is the process for creating a modal transition?

I am attempting to add animation to a modal using a transition effect. My goal is to open it slowly, either from the center of the screen or from the bottom. However, I am struggling to understand how this can be achieved... While searching on Google, I c ...

The issue of linking .then() functions

I've been struggling to grasp the concept of Promises/then-ables for a while now. Currently, I am working with NextJS. My goal is to chain together the following steps: Retrieve data from an API Save the data Modify the data to create a component (w ...

The controller is unable to provide the output in JSON format within the AJAX response

My task requires me to continuously call and fetch data from a REST API every second. To achieve this, I am calling the method with a time interval of 1 second as shown below: var myVar = setInterval(function(){ getData1() }, 1000); Below is the JavaScri ...

React SVG not displaying on page

I am facing an issue with displaying an SVG in my React application. Below is the code snippet: <svg className="svg-arrow"> <use xlinkHref="#svg-arrow" /> </svg> //styling .user-quickview .svg-arrow { fill: #fff; position: ...

difficulty receiving the information promptly via an AJAX request (utilizing AJAX and the 'for' loop)

Currently, I am successfully retrieving data from an ajax call for individuals. However, my next task is to retrieve multiple sets of data simultaneously. Here is the code snippet: for(var i = 1; i <= 2; i++){ console.log(i); $.ajax({ url: cal ...

Exploring methods to verify a service subscription to a topic provided by a different service

My services provide the following functionalities: @Injectable({ providedIn: 'root' }) export class Service1 { dataHasChanged = new Subject(); private data1; private data2; constructor() {} getData() { return { data1: th ...

Obtain the identification number and full name from the typeahead feature

I am able to retrieve the name and ID, but I am only able to display the name. I have attempted using update and afterslected methods. Currently, I have this code which works well, however, it only fetches the name! $('input.typeahead').typea ...

extracting information from XML datasets

XML Sample: <liste> <okul>kod="1" durumid="6" genelid="11702" adi="istanbul lisesi"/> <puan>genelpuan"2" sosyalpuan="0" matpuan="3"/> <okul>kod="6" durumid="8" genelid="11345" adi="ankara lisesi"/> <puan>genelpuan"4" ...

Retrieving JSON data from a URL using Volley within an Android Fragment

Within one of my Fragments, I am attempting to parse JSON data using Volley from a specific URL. However, the issue lies in the fact that the "onResponse" method is not being triggered. I have attempted to catch any exceptions using Log and Toast Notificat ...

Selenium on Sauce Labs does not successfully load the page in Firefox after clicking

An issue has arisen where a test that functions properly with selenium webdriver locally is timing out when executed remotely on saucelabs.com. Notably, the test runs smoothly for Chrome in both local and remote scenarios. The problem seems to lie in the ...

React Ant Design: Toggle Visibility of Columns

Seeking a method to toggle the visibility of columns for the Table Component in Ant Design. The goal is to include a checkbox next to each column name. When unchecked, the column should be hidden. In my previous experience with react-table, accomplishing ...

Having difficulty updating the state with editorState retrieved from the server in ReactJs when using draftJs

Incorporating a rich text editor into React using draftJs has been successful. The editorState data is converted to raw data, stringified, and sent to the server for rendering on the front end. However, I am facing challenges in updating the editorState ...

Displaying an HTML string on a webpage

I am developing a user dashboard using Django for a Python-based web application. This web application generates emails, and the HTML content of these emails is stored in a file (and potentially in a database table as well). As part of the dashboard's ...

What is the process for accessing a particular field in the data returned by the collection.find method in Expressjs and mongodb

I've been attempting to access a specific field returned from the mongodb collection.find method without success. The console.log is not displaying anything. router.get('/buildings', function(req, res, next) { var db = req.db; var collectio ...