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

Mastering the art of scrolling and selecting items concurrently using the mouse in Vue

Struggling with a fascinating challenge of scrolling while selecting items using mouse drag in both up and down directions. Here's a screenshot for reference: https://i.stack.imgur.com/giMwY.png Check out my code: https://codesandbox.io/s/select-i ...

Error 400 encountered in Spring MVC with Ajax

I am working on a Java application using Spring MVC and encountering some issues with sending AJAX requests to a Spring controller. I have noticed that when I include the headers "Accept", "application/json" and "Content-Type", "application/json;charset=ut ...

javascript strange behavior observed with multidimensional array

Looking to create a jquery autocomplete input that responds to the user's input from a previous field. I have a php script that returns a json variable, but I'm having trouble setting up my array correctly afterwards. I've attempted settin ...

Tips for choosing elements in JavaScript using querySelector even after they've been included in the code using innerHTML

Within the scenario below, a parent element is present in the HTML code and the span element with a class of 'child' is nested within the parent element using the createChild function. Subsequently, the content of the child element is modified el ...

Determine if an object has been submitted in a MEAN / AngularJS application

I am working with an AngularJS frontend and using Express, Node, and MongoDB on the backend. My current setup is as follows: // my data to push to the server $scope.things = [{title:"title", other properties}, {title:"title", other properties}, {titl ...

How come eslint is unable to detect all files within a directory, yet it can detect a single file?

Here is the organization of my files and directories. https://i.stack.imgur.com/YWNw3.png I am looking to set up some eslint rules for my code detection. In my .eslintrc file, I have included these configuration settings. { "extends": "airbnb", "ru ...

Inject environment variable into SCSS file while using webpack

I'm new to webpack and I need help with reading a specific value, which is the env variable from the webpack.config.js file, in a sass file. This will allow me to have different CSS styles based on the environment. For example: If the env is set to ...

Ways to extract particular pieces of data from complete JSON response API requests

I need assistance extracting only the Symbol and Company Name fields from a large JSON dataset while retrieving all data. How can I achieve this and then save the extracted information into a pandas DataFrame? Base_url Here is the code snippet: import re ...

Utilizing jQuery Autocomplete to access JSON data via an API

My understanding of jQuery autocomplete is a bit limited, so I'm hoping for some guidance on how to achieve my task. I currently have the following URL: http://localhost/contactApi.do?mobile=614321 This URL allows for inputting either a complete or ...

Struggling to make cookies stick in IE9

Here is the code snippet I am currently using: <script> var time = new Date(); time.setFullYear(time.getFullYear() + 1, time.getMonth(), time.getDay()); expires = ";expires=" + time.toGMTString(); document.write(expires); doc ...

Tips for updating a specific portion of a component in react.js

import React,{useEffect} from 'react'; import CloudTables from '@cloudtables/react'; import { useState } from 'react'; function DataGridTable ({ input1Value, input2Value }) { return ( <div className="con ...

Each time I invoke the setInterval function, my counter speeds up - using vuejs

In my development process, I am creating a countdown that is triggered by a function. The main objective is to reset the countdown each time a user answers a question in the game and a new question appears. However, I have encountered a challenge where i ...

Guide on updating property or field names within a personalized JSON serializer

I have created a custom serializer for a specific field. public class SearchSerialize extends StdSerializer<Value> { public SearchSerialize() { super(Value.class); } @Override public void serialize(Value value, JsonGenerator jsonGenerator, Seri ...

.ajax() triggers a page refresh upon pressing the ENTER key

Utilizing Ajax to update the database with a new folder leads to page refresh when hitting ENTER. On the form, I have onkeypress="if(event.keyCode==13) savefolder();". Below is the Javascript code where pressing enter calls savefolder function that sen ...

Issue with Context Menu Not Triggering on Dynamically Added Elements in JQuery

Check out the JSFiddle Demo Within my email sidebar, I implemented a custom right-click feature that allows users to add new sub-folders. The code snippet below demonstrates how this functionality works: if ($(this).hasClass('NewSubFolder')) { ...

My content is being obstructed by a single-page navigation system

I attempted to create a simplified version of the issue I am facing. Basically, I am working on a header with navigation that stays at the top of the page while scrolling. The problem arises when clicking on a section in the navigation. The screen scrolls ...

Implementing a Popover Notification When Clicked

I'm a beginner at this. I came across an example of a popover message box in the link provided below. I attempted to implement it, but for some reason, it's not working. Could I be overlooking something? Alternatively, is there a simpler way to ...

Utilizing Angular's ng-repeat directive to dynamically generate images, with the added functionality of attempting to

I am utilizing angular-drupal to fetch data from my backend built on Drupal. My objective is to create an image gallery using the default endpoints provided by the services module. I make a call to node load to retrieve a specific node, then I extract the ...

Personalized tooltips for numerous data sets in Highcharts

I am currently in the process of constructing a highchart that is capable of accommodating up to five different types of data series. I have arranged similar series together, resulting in three distinct y-axes for the various series. However, I have encou ...

Modifying the appearance of radio buttons using jQuery

I'm new to jQuery and I'm finding it challenging. Currently, I have a set of three radio buttons with the third button already prechecked. My aim is to change the CSS class of the checked button, making it similar to an unchecked button with the ...