Determine if values in an array are present in an array of objects using JavaScript

Greetings, I currently have an array and an array of objects.

const factArr = ['743156', '743157']

[
    {
        "id": null,
        "name": null,
        "adsFactId": "743156"
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743157",
    },
    // other object data here...
]

I am looking to verify if the values in factArr exist within this object, matching the adsFactId. If they do, I want to add two new properties to that object:

checked:true, checkBoxPatched: true

Hence, the desired output should be:

[
    {
        "id": null,
        "name": null,
        "adsFactId": "743156",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743157",
        "checked": true,
        "checkBoxPatched": true
    }
    // along with other objects
]

Is there a way we can accomplish this task?

Answer №1

const factArr = ['743156', '743157'];
const dataList = [
    {
        "id": null,
        "name": null,
        "adsFactId": "743156"
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743157",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743158",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743159",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "743976",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "744961",
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "746809"
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "747229"
    },
    {
        "adsId": null,
        "name": null,
        "adsFactId": "747231"
    },
    {
        "id": null,
        "name": null,
        "adsFactId": "749059"
    }
];

dataList.some(obj => {
  if(factArr.includes(obj['adsFactId'])) {
    obj.checked = true;
    obj.checkBoxPatched = true;
  }
});
console.log(dataList);

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

Creating an iPad-inspired password field experience in a text input with jquery: A step-by-step guide

Looking for a plugin that mimics the iPad's password field behavior for credit card number entry. The focus should only reveal the entered digit and then switch to a bullet as the next digit is typed. Additionally, all previously entered digits should ...

Tips for delaying the evaluation of an <input> field?

I am interested in analyzing the content of an <input> field when there is no activity from the user. One simple example I have considered is counting the number of characters, but the actual analysis process is very resource-intensive. To optimize ...

Can we tap into the algorithm of curveMonotoneX in d3-shape?

I'm currently using curveMonotoneX to draw a line in d3 import React from 'react'; import { line, curveMonotoneX } from 'd3-shape'; export default function GradientLine(props) { const { points } = props; const lineGenerator ...

What is the best way to define a function that declares and returns an array for global use

I'm attempting to globally declare an array from a function so that it can be accessed by other functions as well. However, I'm unsure how to do this because the code involves a csvtojson converter which complicates things. I'm wondering if ...

Ways to invoke a C# server-side function using JavaScript

I'm looking to invoke a C# server method from JavaScript. Within my gridview, there is a column containing dropdown lists. When the value of the dropdown list changes, I need to use JavaScript to call a server-side method and update the value of anoth ...

Printing an array of objects using Java

Despite the numerous resources available on this topic, I'm finding it difficult to comprehend in my specific scenario. The task at hand is to print an array of objects. For instance, I have an array containing objects from the "shape" class. Should ...

Using PHP and MySQL to create checkboxes populated with data retrieved from a database, and then submitting two parameters

Hey there, I've been struggling with this issue for quite some time. I'm trying to create a list with checkboxes populated from a database that includes [checkbox of car id and value][brand][model]. The goal is to select two cars from the list, ...

Fixed Container housing a Child Div with Scrollbar

I am faced with a challenge involving a lengthy navigation menu (.nav-main) nested within a fixed header (header). When the navigation menu is toggled using jQuery .toggle(), the content extends beyond the window height and does not scroll. I am looking fo ...

React throws an error message when the update depth surpasses its maximum limit

I am facing an issue with my container setup where the child container is handling states and receiving props from the parent. The problem arises when I have two select statements in which onChange sets the state in the child container, causing it to re-re ...

From converting Javascript code to PHP and using the innerHTML function for deletion

I'm currently working on implementing a script and could use some assistance with two specific issues that I'm struggling to resolve. The main goal is to enable users to create a running route and then store the route in a database using coordina ...

Converting a string to regular text in JavaScript (specifically in ReactJS)

When I fetch data from an API, sometimes there are special characters involved. For example, the object returned may look like this: { question : "In which year did the British television series &quot;The Bill&quot; end?" } If I save t ...

Is there a way to perform a nested json query search in MySQL?

I am currently utilizing MySQL version 5.7 and above which supports the native JSON data type. Here is a snippet of sample data for reference: set @jsn_string='{\"body\": {\"items\": \"[{\\\"count\\& ...

The output from the toJson method of the Gson library contains invalid JSON

When using the toJson method to generate JSON from an object, I recently ran into some issues with translating the object into the JSON view. SingnUpRq singnUpRq = new SingnUpRq("lsjdflkdjslfk", "ZCqKckIpwvxtLFZz3TlUln5RqUpEB5U43imi7PSYv6UBY/bLvKQBSDI ...

How can React with TypeScript dynamically extend type definitions based on component props?

Can a React component dynamically determine which props it has? For example: type BaseType = { baseProp?: ... as?: ... } type Extended = { extendedProp?: ... } <Base /> // expected props => { baseProp } <Base as={ExtendedComponent} ...

Troubleshooting a Django TypeError while parsing JSON: A step-by-step guide

After running the code below, I am encountering a TypeError in the browser. The error specifically occurs at the final line with the message 'NoneType' object is not subscriptable (I am attempting to retrieve all the URLs for each item). What&apo ...

Removing a dynamic TypeScript object key was successful

In TypeScript, there is a straightforward method to clone an object: const duplicate = {...original} You can also clone and update properties like this: const updatedDuplicate = {...original, property: 'value'} For instance, consider the foll ...

When using Inertia.js with Laravel, a blank page is displayed on mobile devices

Recently, I've been working on a project using Laravel with React and Inertia.js. While everything runs smoothly on my computer and even when served on my network (192.168.x.x), I encountered an issue when trying to access the app on my mobile phone. ...

A comprehensive guide on using HttpClient in Angular

After following the tutorial on the angular site (https://angular.io/guide/http), I'm facing difficulties in achieving my desired outcome due to an error that seems unclear to me. I've stored my text file in the assets folder and created a config ...

Utilize Play Framework version 2.1 in combination with reactivemongo version 0.8 to tally

I am currently working on a Play 2.1 application that utilizes MongoDB through the Reactivemongo 0.8 plugin. In my project, I have implemented an approach outlined here without using models. One of the methods in my application retrieves all documents fro ...

Issue with Android date input field not resetting the value

When testing in Android Tablet (version 4.1.2), I encountered a strange issue with an input date field that looks like this: <input type="date" value="2013-06-10"/>. Upon clicking on the field and trying to set a new date, instead of replacing the o ...