Determine the position of the parent array based on the value of a child array within a set of

Is it possible to retrieve the index of the parent based on a specific element within the children? For instance, consider the following structure stored in a variable called list:

[
    [
        {id: "1", contactType: {id: "phoneNumber", company: {id: "01", name: "Company01"}}, value: "5555555555"},
    ],
    [
        {id: "2", contactType: {id: "phoneNumber", company: {id: "03", name: "Company03"}}, value: "7777777777"},
        {id: "3", contactType: {id: "phoneNumber", company: {id: "05", name: "Company05"}}, value: "8888888888"},
    ],
]

I attempted to use the includes and findIndex methods to determine if a specific element exists and then capture the index of its parent:

list.includes('5555555555', 0);

My expectation was to receive true as I instructed includes to search from index 0 where the element "5555555555" is located. Unfortunately, the result was false.

Another approach involved:

list.findIndex(x => x.value === '5555555555');

The anticipated outcome was 0 since the element 5555555555 resides within the first array. Nevertheless, the result returned was -1.

An alternative attempt using flat() to access the children and subsequently applying includes led to the loss of reference to the original indexes 0 and 1 in the initial list.

Desired result:

foo(list,'7777777777'); should yield 1 as this element is situated within the second array of the list. Similarly, foo(list,'8888888888'); should also produce 1 since the element is contained within the same array.

Any guidance on resolving this issue would be greatly appreciated.

Answer №1

Implementing Array.findIndex with a specific condition

const records = [
[
    {id: "1",contactType: {id: "phoneNumber",company: {id: "01",name: "Company01"}},value: "5555555555"},
],
[
    {id: "2",contactType: {id: "phoneNumber",company: {id: "03",name: "Company03"}},value: "7777777777"},
    {id: "3",contactType: {id: "phoneNumber",company: {id: "05",name: "Company05"}},value: "8888888888"},
],
];

const dataIndex = records.findIndex(item => item.findIndex(node => node.value === "5555555555") > -1);
console.log(dataIndex)

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

Update the regular expression pattern to extract nested tags and store them in an array of objects

For my small application, I am working on creating a regex pattern to identify "faux" html tags. The goal is to capture these tags within curly brackets and output them into an array of objects. Below is the code snippet with the current regex pattern: { ...

Attempting to enhance the modularity and readability of my code

Looking for assistance to enhance the modularity and readability of this lengthy code. Any tips on how to simplify it and improve clarity would be greatly appreciated! I'm currently working on a street fighter game, and here's what I have so far ...

Converting a string into an array of doubles

I have a challenge where I need to transform a string: Dim tmpTry As String = "10, 20, 30, 40, 50, 52, 20, 20, 10, 35, 3, 8, 47, 7, 2, 5, 55, 8, 0, 0, 6, 55, 0, 2, 12, 0, 0, 21, 14, 0, 3" Into a double array: Dim arrNumOfVisits As Double() = New Double( ...

The jQuery selector for items that do not contain

Have you ever wanted to hide list items based on user input in a text box? The ":contains()" jQuery selector can help you achieve this by allowing you to match elements containing specific text. I have a similar goal - I want users to be able to type in a ...

Creating an array dynamically by parsing a JSON object in Javascript

I am currently working with a JSON object that looks like this: var testJSON = [ { "AssetA": "asset_a", "AssetB": "asset_b" }, { "AssetA": "asset_a", "AssetB": "asset_b" }, { "AssetA": "asset_c", "AssetB": "asset_d" }, { "AssetA": "asset_c", " ...

Tips for customizing video layout using HTML

I currently have a basic video displayed on my website <video width="100%" class="posted_vid"> <source src="uploaded_videos/<?php echo $Video; ?>"> </video> However, the video player appears as a default HTML video, simila ...

Develop an onclick function with an href attribute

I am interested in transforming links into AJAX versions whenever possible. To achieve this, I plan to implement a function called replaceLinks that will add an onClick handler to each link on the page and trigger ajaxPageWSM(href). This is what I currentl ...

Creating a row of aligned vertical sliders using jQuery UI

I'm having trouble aligning multiple vertical UI sliders on the same line. Here's what I'm looking to achieve: 1. Have 4 vertical sliders displayed in a row. 2. Show numerical values as the user moves each slider. The code I'm currentl ...

Firefox not triggering image onload event

I have developed an image preloader that is functioning perfectly in all browsers except for Firefox (currently tested with version 10.0). Essentially, the input consists of an array of images named image_list. These images are dynamically appended to the ...

What is the best way to invoke a TypeScript function within a jQuery function?

Is it possible to invoke a TypeScript function within a jQuery function? If so, what is the correct approach? Here is an example of my component.ts file: getCalendar(){ calendarOptions:Object = { height: 'parent', fixedWeekCount : ...

The VBA Filter function encounters a runtime error 13, specifically a type mismatch error, when applied to an array generated from a range

Currently, I am attempting to search for a specific substring within an array using the filter function and then creating a new array of strings containing that substring. However, when I try to execute this, I encounter a type mismatch runtime error 13. I ...

JavaScript, detecting repeated characters

I need to create a script that checks an input box (password) for the occurrence of the same character appearing twice. This script should work alongside existing regex validation. I believe I will need to use a loop to check if any character appears twice ...

Utilize the Webpack library and libraryTarget settings to establish our own custom library as a global variable

I currently have a library named "xyz" that is being imported as a node module from the npm registry. Now, I want to incorporate it as a library and make it accessible under the global name "abc". To achieve this, I plan to utilize webpack configuration. ...

What is the best way to retrieve accurate error messages using AJAX and JSON?

Storing span values into a database has been successful. However, I am encountering an issue with the ajax return error message. For instance, in my save.php code, I mistakenly changed my table name from sample to simple (which does not exist in my databas ...

The click function is only functioning for the most recently added div, failing to work on all previously appended divs in jQuery

Thank you to all the individuals who take the time to read my post and provide assistance. I am currently working on a select picker that displays the selected options below the select using ajax. However, I have encountered an issue where the 'x&apos ...

Discovering the row and column with the highest number of 1's in a 2D array

I am currently working on a program that involves randomly inserting 0's and 1's into a 4 x 4 array matrix. The objective is to identify the column and row index with the highest occurrences of the number 1. While my row scanning method seems to ...

I feel like I'm stuck in a never-ending loop of COR issues that are blocking my progress on

My main focus is establishing a direct connection between a computer and a Raspberry Pi within a local network. It's important to clarify that this project will never have any connection to the internet. The Raspberry Pi is running a NodeJS server th ...

Button-controlled automated presentation

I have devised a method to create an automatic slideshow using JavaScript. However, I am looking to incorporate manual control buttons as well. After adding manual control code, the slideshow seems to be malfunctioning. The provided code below is used for ...

The program encountered a 'TypeError: education.map is not a function' error when trying to generate the page. All console logs related to this issue will be shown in

Hey everyone, I'm currently working on a project in Next.js with a main file named Project.js. I'm running into some issues trying to export data from the main file to an object called Resume for the education section component... import EduSecti ...

Creating a Dynamic Nested Associative Array in PHP

I am working with an associative array that is generated dynamically using values from a database. When I use print_r($array) to print the whole array, it shows something like this: Array ( [95a5c80811239526fb75cbf31740cc35] => Array ( [product_id] =&g ...