Utilizing the array.map method to access the key of an object within an array of arrays in TypeScript

Can I utilize array.map in TypeScript to apply a function with the parameter being the key of an object within an array? I have an array containing objects which have keys 'min' and 'max'. I am looking to use something like

someArrayFunction(array.map(func1(key), array.map()))
to compare the values of each key in the object array.

const allVariables: any[] =  [
  [ { name: 'a', value:{min: 1, max: 2}, expect: {min: 1, max: 2}}, { name: 'b', value: {min: 11, max: 22}, expect: {min:11, max: 22} }],
  [ { name: 'a1',value:{min: 1, max: 2}, expect: {min: 1, max: 2}}, {name: 'b1', value: {min: 11, max: 22}, expect: {min:11, max: 22} }],
  [ { name: 'a2',value:{min: 1, max: 2}, expect: {min: 1, max: 2}}, {name: 'b2', value: {min: 11, max: 22}, expect: {min:11, max: 22} }],
];

allVariables.forEach(obj => {
  Object.keys(obj).forEach((key) => {
    // someArrayFunction(someFunc(obj.name, key), obj.value[key])
    // comparing value.min === expect.min for name a
    // comparing value.min === expect.min for name b
    // comparing value.min === expect.min for name a1
    // comparing value.min === expect.min for name b1
    // comparing value.min === expect.min for name c1

  })
})

// someArrayFunction(parameter: string[], v: number){
  // do something with parameter and v...
// }

Answer №1

I'm not entirely sure if that's what you were looking for.

const allItems: any[] =  [
  [
    { name: 'x', value:{min: 3, max: 5}, expect: { min: 3, max: 5 } },
    { name: 'y', value: {min: 8, max: 11}, expect: { min:8, max: 11 } },
  ],
  [
    { name: 'x1',value:{min: 2, max: 4}, expect: { min: 2, max: 4 } },
    { name: 'y1', value: {min: 7, max: 12}, expect: { min:7, max: 12} },
  ],
  [
    { name: 'x2',value:{min: 6, max: 9}, expect: { min: 6, max: 9 } },
    { name: 'y2', value: {min: 10, max: 15}, expect: { min:10, max: 15 } },
  ],
];

allItems.forEach(group => {
  group.forEach(item => {
    const {value, expect} = item;

    console.log(anotherArrayFunction(item, value));
  })
})


function anotherArrayFunction(value, expect) {
  return value.min === expect.min;
}

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

What is the best way to specify a type for an object without altering its underlying implicit type?

Suppose we have a scenario where an interface/type is defined as follows: interface ITest { abc: string[] } and then it is assigned to an object like this: const obj: ITest = { abc: ["x", "y", "z"] } We then attempt to create a type based on the valu ...

What is the best way to invoke a function with a variable number of arguments?

I am struggling with passing each element of an array as parameters to a function. For example: $myArray = array("element1","element2","element3"); //Pass each element as a new parameter to a function myFunction($element1, $element2, $element3); //If th ...

Merge two arrays with identical keys

I currently have two arrays: $sele_itmid = Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 4 [4] => 4 ) $fltrd_serl_batch = Array ( [0] => SAL121 [1] => SAL122 [2] => SAL 145 [3] => M121 [4] =&g ...

An issue occurred with Ionic 4: TypeError - Unable to access property 'name' as it is undefined

None of the answers to similar questions have provided a solution for me SITUATION: I've been setting up a SQL Server connection from my Ionic app. You can check out my previous question for more details The workflow goes as follows: Ionic connects ...

Encountered an error in production mode with Angular 7: Uncaught ReferenceError - "environment" variable

During development, my application runs smoothly, and ng build --prod --source-map successfully compiles the application. However, when attempting to access it through the browser, an error occurs: app.module.ts:47 Uncaught ReferenceError: env is not defi ...

The type 'FileUpload[][]' cannot be assigned to the type 'AngularFireList<FileUpload[]>'

I'm currently working on an Angular application integrated with Firebase for the purpose of uploading images to the database and retrieving them as well. upload-file.service.ts import {Injectable} from '@angular/core'; import {AngularFireD ...

How can I showcase Google images on my website?

I'm currently working on a project where I want to showcase images on my website retrieved from Google Images based on a keyword stored in an array. Here's the code I have so far: <?php $lines = file('things.txt'); echo $lines[arra ...

Angular JSON converter - Transform XML data to JSON format

Struggling to convert XML API response to JSON using xml2js library, facing issues with getting 'undefined' in the console. Here is my API service: export class WordgameService { public apiUrl = "http://www.wordgamedictionary.com/api/v1/reference ...

Dismantling an array into individual elements

I'm struggling to figure out what I'm doing wrong in this code. My goal is to output an array from a variable into its separate items. For instance: $current_endMaps = get_post_meta($post->ID, "_tsb_postmeta_end_maps", true); This gives me ...

Error: Unable to locate the tslint command

After attempting to utilize tslint --fix, I encountered the error message bash: tslint: command not found.... To install tslint, I ran the following command: yarn global add tslint typescript. The operating system on my machine is Centos 7. ...

An instance of an abstract class in DI, using Angular version 5

I have multiple components that require three services to be injected simultaneously with the same instance. After that, I need to create a new instance of my class for injecting the services repeatedly. My initial idea was to design an abstract class and ...

Create Office Script calculations with quotations included

I am currently attempting to create an Excel office script formula for a cell. Are there any tips on how to insert a formula with quotes into a cell? For example, the following works fine: wsWa.getCell(WaRangeRowCount, 9).setFormula("= 1 + 1"); ...

The attribute 'split' is not found on the never data type

I have a function that can update a variable called `result`. If `result` is not a string, the function will stop. However, if it is a string, I then apply the `split()` method to the `result` string. This function always runs successfully without crashin ...

Ionic causing delay in updating ngModel value in Angular 2

My ion-searchbar is set up like this: <ion-searchbar [(ngModel)]="searchQuery" (keyup.enter)="search();"></ion-searchbar> In the typescript file, the searchQuery variable is defined as follows: export class SearchPage { searchQuery: string ...

Supertest and Jest do not allow for sending JSON payloads between requests

Below is the test function I have written: describe("Test to Create a Problem", () => { describe("Create a problem with valid input data", () => { it("Should successfully create a problem", async () => { const ProblemData = { ...

Angular release 6: A guide on truncating text by words rather than characters

I'm currently enhancing a truncate pipe in Angular that is supposed to cut off text after 35 words, but instead it's trimming down to 35 characters... Here is the HTML code: <p *ngIf="item.description.length > 0"><span class="body-1 ...

a callback may seem like it's not a function, but in reality, it is

This question might seem simple, but I'm struggling to grasp the concept of callbacks, especially in NodeJS. My issue arises when trying to retrieve data from MySQL, something that is usually straightforward in most programming languages: In my rout ...

Trouble with displaying points in Angular2's highcharts

I have implemented the angular2-highcharts chart module in my angular2 web application. Everything works fine when the graph has less than 7000 points, with the line and points displaying correctly. However, once the number of points surpasses 7000, there ...

Aggregate the data chunks in the antd table into groups

I've been struggling to find a solution for this specific issue. My goal is to group the data in my table based on a particular field, but I need it to be styled as depicted in the image linked below. https://i.stack.imgur.com/OsR7J.png After looking ...

A guide on assigning specific (x, y) coordinates to individual IDs within the tree structure

When attempting to calculate the positions of each ID in order to arrange them hierarchically on the canvas, I encounter some challenges. Whether it's organizing them into a tree structure or multiple trees resembling a forest, one restriction is that ...