Mix arrays and include a marker if it aligns with the identification number

Two arrays with the same structure have been created.

let TaskArray=[
  {"TaskID:"171","TaskName":"task1","TaskGroup":"group","UserID":"3"},
  {"TaskID:"170","TaskName":"task2","TaskGroup":"group","UserID":"3"},
  {"TaskID:"169","TaskName":"task3","TaskGroup":"group","UserID":"3"},
  {"TaskID:"168","TaskName":"task4","TaskGroup":"group","UserID":"3"},
  {"TaskID:"167","TaskName":"task5","TaskGroup":"group","UserID":"3"},
  {"TaskID:"166","TaskName":"task6","TaskGroup":"group","UserID":"3"},
  {"TaskID:"165","TaskName":"task7","TaskGroup":"group","UserID":"3"},
  {"TaskID:"164","TaskName":"task8","TaskGroup":"group","UserID":"3"},
  {"TaskID:"163","TaskName":"task9","TaskGroup":"group","UserID":"3"},
  {"TaskID:"162","TaskName":"task10","TaskGroup":"group","UserID":"3"}
]

let TaskDetailsArray = [
  {"TaskID:"171","TaskName":"task1","TaskGroup":"group","UserID":"3"},
  {"TaskID:"170","TaskName":"task2","TaskGroup":"group","UserID":"3"},
  {"TaskID:"169","TaskName":"task3","TaskGroup":"group","UserID":"3"},
  {"TaskID:"168","TaskName":"task4","TaskGroup":"group","UserID":"3"},
]

A comparison needs to be made between the two arrays, and a flag named isAssigned should be set to true if an item from the second array is found based on its ID in the first array; otherwise false.

matcheArray = [
  {"TaskID:"171","TaskName":"task1","TaskGroup":"group","UserID":"3",isAssigned: true},
  {"TaskID:"170","TaskName":"task2","TaskGroup":"group","UserID":"3",isAssigned: true},
  {"TaskID:"169","TaskName":"task3","TaskGroup":"group","UserID":"3",isAssigned: true},
  {"TaskID:"168","TaskName":"task4","TaskGroup":"group","UserID":"3",isAssigned: true},
  {"TaskID:"167","TaskName":"task5","TaskGroup":"group","UserID":"3"},
  {"TaskID:"166","TaskName":"task6","TaskGroup":"group","UserID":"3"},
  {"TaskID:"165","TaskName":"task7","TaskGroup":"group","UserID":"3"},
  {"TaskID:"164","TaskName":"task8","TaskGroup":"group","UserID":"3"},
  {"TaskID:"163","TaskName":"task9","TaskGroup":"group","UserID":"3"},
  {"TaskID:"162","TaskName":"task10","TaskGroup":"group","UserID":"3"}
]

The current code functions as intended, but there may be alternative approaches. Any suggestions for improvement would be appreciated.

   for (var i = 0; i < TaskArray.length; i++) {
                        for (var k = 0; k < this.TaskDetailsArray.length; k++) {
                            if (TaskArray[i].TaskID == this.TaskDetailsArray[k].TaskID) {
                               
                                if (!this.TaskDetailsArray[k].isAssigned) {
                                    this.TaskDetailsArray[k].isAssigned = true;
                                };
                            }
                        }

Answer №1

let updatedArray = taskArray.map(item => {
   return {
...item,
isAssigned: TaskDetailsArray.some(secondElement => secondElement.TaskId === item.TaskId );
  }
})

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 shopping list feature is unable to save or list multiple recipes at once

My goal is to: Create a shopping list that retrieves recipes from an API. Transfer ingredients from one page to another. Automatically refresh/load data when more than 1 item is added. The challenge I am facing is: Only one set of ingredients loads. T ...

What is the best method for transferring data stored in a Python array to an Excel file?

Currently, I am in the process of developing a python script that handles .hdf files. My goal is to extract the data and store it in an excel spreadsheet. I have successfully stored the data in an array using the following code snippet: See the code below ...

Refreshing the browser causes Angular route to display raw JSON data

I have been working on a MEAN stack application and I have set up a proxy configuration for development purposes. Everything seems to be in order as I am able to successfully call an API from Angular/Node. However, the issue arises when I refresh the brows ...

Encountering an issue with extending the MUI color palette, receiving a "reading 'dark'" error due to properties of undefined

Encountering an issue when trying to expand the MUI color palette—getting this error instead: Error: Cannot read properties of undefined (reading 'dark') Take a look at my theme.ts file below: const theme = createTheme({ palette: { pri ...

Issue with typescript: "Property not found in void type"

So, I've stumbled upon some code I created using Angular Bootstrap for a date picker within an event registration form (where you click new to create a new event). The objective is for the user to select a date and save it. Here's the code snippe ...

The conclusion from utilizing the TypeScript class factory mixin is that it does not yield a constructor function

While attempting to utilize mixin classes in TypeScript, I encountered an issue. The return value of the mixin application (Sizeable.mixin() in the following code) is reported as "not a constructor function". However, it is puzzling because the error outpu ...

When faced with the error message "Typescript property does not exist on union type" it becomes difficult to assess the variable

This question is a continuation of the previous discussion on Typescript property does not exist on union type. One solution suggested was to utilize the in operator to evaluate objects within the union. Here's an example: type Obj1 = { message: stri ...

What is the method for obtaining the current participant's ID from the Angular frontend of a Hyperledger Composer application?

Need help with hyperledger-composer: I know how to retrieve the id of the current participant within a transaction processor function using this code: let currentParticipant = getCurrentParticipant(); let participantId = currentParticipant.getFullyQuali ...

What consequences can I expect for my memory when using C arrays?

Hey there, readers! I'm curious to know what will occur in my computer when I execute this incorrect code using the GNU GCC compiler in CodeBlocks. The following is erroneous code: char data[5]; data[0] = '1'; data[1] = '10&ap ...

Angular enables the use of multiple instances of a service from the parent component to the child component

I recently came across this discussion: Utilizing multiple instances of the same service. Can one utilize multiple instances of a service from parent to children? For instance, imagine having an ElementService in the ParentComponent with 2 separate instan ...

Comparable to LINQ SingleOrDefault()

I frequently utilize this particular pattern in my Typescript coding: class Vegetable { constructor(public id: number, public name: string) { } } var vegetableArray = new Array<Vegetable>(); vegetableArray.push(new Vegetable(1, "Carrot")); ...

Receiving an error in Angular 5 that says a differ cannot be found to support the object '[object Object]' of type 'object'

Representation of the result object data: { "23": { prop: abc, prop_2: def }, "78": { prop: abc, prop_2: def }, "2098": { prop: abc, prop_2: def }, } Sample Code Snippet <div *ngFor="let filterCar of data"> ...

Tips for resolving: Dilemma - Angular configuration loaded in both synchronous and asynchronous manner

I recently updated my Angular 6 project to Angular 11. The project includes server-side rendering (SSR), and I am encountering an issue. After running ng run myapp:server, I am getting the following error: ✔ Server application bundle generation complete ...

Traverse on a diagonal path within a two-dimensional grid

I am facing a dilemma in my maze adventure - how do I move ne or sw when I can only go north, east, south, or west? Can anyone provide examples using my code? It's a 4x4 array. Here's the user interface: //Begin user dialog System.out.p ...

What kind of Antd type should be used for the form's onFinish event?

Currently, I find myself including the following code snippet repeatedly throughout my project: // eslint-disable-next-line @typescript-eslint/no-explicit-any const handleCreate = (input: any): void => { saveToBackend({ title: input.title, oth ...

What is the best way to restrict the key of an object type to only be within a specific union in TypeScript?

I need to create a set of server types in a union like this: type Union = 'A' | 'B' | 'C'; After that, I want to define an object type where the keys are limited to only certain options from this Union: // Use only 'A&ap ...

Techniques for adding values to arrays in JavaScript

My Anticipated Result let Album = { album_desc: AlbumTitle, id: 1, album_title: 'ss', AlbumDescription: 'sdsd', ImageName: 'image.jpg', album_pics: below array }; I am looking to dynamically p ...

Transform a single attribute in an array of objects using angularjs and Javascript to a different value

Within an angularjs controller, the following code is used: var ysshControllers = angular.module('theControllers', []); ysshControllers.controller('CommonController', function($scope, $http, $route) { $scope.dbKey = $route ...

Become a Member Of a Nested Array [Using PHP and MYSQL]

How can I efficiently join multiple tables and easily create a nested array in PHP? For example: Table 1 -School: SchoolID, SchoolName,PrincipalID Talbe 2 - Principal: PrincipalID,PrincipalName I would like to generate a nested array in PHP similar to: ...

Using xlwings for dynamic arrays is leading to formulas being encompassed with curly braces

Having trouble creating a dynamic array formula in Excel using xlwings? Every time I attempt to input the formula, it either adds "@" after "=", or encloses the formula within "{}". Here is the code snippet causing the issue: def process_files(): # Dis ...