Retrieve the value stored within an array object

I am dealing with the following data structure:

var myValues = {
  55bdf7bda89de40349854077: ["hello"]
  55be0c77a89de403498540bc: ["goodbye"]
  55be0e22a89de403498540c1: ["hey there!"]
}

Also, I have a variable that holds an id:

var id = '55be0e22a89de403498540c1';

My objective is to search for this id in the object and retrieve the corresponding value from the array.

I attempted to locate it using:

myValues.id[0]

However, this approach did not yield the desired outcome.

Is there anyone who can assist me with this issue?

Answer №1

To retrieve the values from the JSON object, use either myValues[id] or

myValues['55be0e22a89de403498540c1']
. This is because it is a JSON object and not a basic array, meaning direct index access like you are attempting will not work.

Answer №2

Make sure your array looks like this:

var myValues = {
  '55bdf7bda89de40349854077': "hello",
  '55be0c77a89de403498540bc': "goodbye",
  '55be0e22a89de403498540c1': "hey there!"
}

alert(myValues["55be0e22a89de403498540c1"])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>

I hope this information proves beneficial to you.

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

Tips on extracting the value from an observable and storing it in a variable

I am facing a challenge in returning a value from an observable method to assign it to a public variable for later use. Specifically, I need to retrieve this value and store it in the 'viewcount' variable for utilization in another function. pub ...

What is the process for incorporating ejs into the source attribute of an image element?

Code Snippet: <img class="card-img-top" alt="..."><%= articles.image %><img> Server Setup: const express = require('express') const app = express() const router = require('./routes/article') app ...

Creating a three-dimensional array in C++

What is the correct way to initialize a 3D array in C++? int array[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; ...

How can I dynamically assign ng-model with a filter for a particular object in an array?

Is there a recommended method for linking an input element to a specific field of an object in an array using its id? I tried using ng-model along with the find() function from the array prototype. However, the implementation is not working properly as it ...

Error 19: Constraint violation - duplicate entry detected

While trying to set up my app, create a local database, and insert the very first user who has logged in locally, I encountered an error message at a specific point in the code. Here's where it happened: angular.module("greenApp") .service("d ...

What could be causing this RangeError to constantly come up while I'm performing a basic math operation in node.js?

My program simply takes two inputs from an HTML form, parses them into text in Node, and then performs a math operation on the two. Strangely, this issue only occurs when dealing with numbers, not strings. Despite the error message indicating an invalid s ...

The rendering of HTML DOM is being obstructed on iPhone devices running iOS 13

Currently, I'm developing a web-based URL stream music player using JavaScript. The player functions smoothly on Android devices, but I'm encountering an issue with DOM rendering being blocked on iPhone devices. Despite rearranging the JavaScript ...

fnRedraw and fnReloadAjax do not have the capability to refresh the datatable

I have been working on updating a table with new data from an ajax url. The table loads correctly the first time, but I am struggling to get it to refresh. $(function() { var datepicker = $( "#date-picker" ); var table = $("#reports1").dataTable( ...

The quick sort algorithm fails to properly sort an array that is already sorted

#include<stdio.h> void swap(int *a, int *b){ int temp = *a; *a=*b; *b=temp; } int partition(int arr[], int l, int r){ int pivot = arr[l]; int left,right; for(left=l+1,right=r;left<right;){ ...

Can you provide me the steps to delete the title attribute from images in Wordpress?

My client has expressed dissatisfaction with the tooltip that appears when hovering over images in certain browsers, particularly Safari. This tooltip displays the title attribute within the img tag, which is a requirement enforced by Wordpress. Even if w ...

I would like to hide the button if there are fewer than five visible

When there are less than 5 visible buttons, I want the button to be invisible. The state visible outputs 5 buttons each time. On the detail page, I would like to have the buttons invisible if there are fewer than 5 outputs. In my current code, when ther ...

Searching through MongoDB with aggregate lookup using numerous "ON" conditions

Seeking help with MongoDB collections that contain arrays and nested arrays, I need to perform a lookup operation using two foreign keys. Here is the structure of the collections: Stocks { merchantIds: ["abc"], products: [ { code: "123 ...

What is the best way to extract JSON data that has already been double quoted in Objective-C?

Can you help me with parsing JSON data that is already double-quoted? For example, say we have received the following JSON: "first_name" = Noah; Here is the code I have tried: //"first_name" = Noah; NSString *name=[result valueForKeyPath:@"first_name"] ...

Retrieve the selected value from a specific div element

Having some issues with retrieving the selected value from a Bootstrap chosen select field within a div. Any ideas on what might be causing this problem? $('#content_' + id).find('#GroupID > option:selected').text() // returns blank ...

Transferring information from ng-Dialog HTML to a controller

Having some issues with ng-Dialog. When I include the ngDialog controller option, it works. I can retrieve the value of $scope.descriptionText from <p>Description:</p> <textarea ng-model="descriptionText"></textarea> Now, whe ...

Why should <template> be used in Vuetify?

Exploring the possibilities of Vuetify 2.0 in my current project has led me to dive into the v-stepper component, designed for displaying progress through numbered steps. In the example provided in the playground, I noticed the use of the <template> ...

Refreshingly modernizing SVG in Angular

I've been facing a challenge in my Angular application where I am trying to dynamically add paths to an SVG element within an HTML file. The issue is that even though the paths are getting added to the DOM, they are not showing up in the browser when ...

What is the best way to save a jQuery or JavaScript variable into a JSON file?

Is there a way to save a jquery variable in a json file? I have the following: var image='/test/test.png'; I am obtaining this path through file upload: <input type="file" name="imageurl" value="imagefile"></input> Therefore, I ne ...

Vue.js is displaying the error message "Expected Object, got Array" when the length appears as undefined

I have an app where users input style codes into a field. I want to create a popup confirmation modal that displays a message with the number of style codes provided. The code I currently have is: <template> <h4>Style Number</h4> <For ...

When using the parseFloat function with a regular expression input, it results

My goal is to adjust float values within a string by a specific amount. Here is my current approach: var string = '<path d="M219.6,-71.4C249,19.1,212.7,130.9,135.7,186.8C58.8,242.7,-58.8,242.7,-135.7,186.8C-212.7,130.9,-249,19.1,-219.6,-71.4C-19 ...