Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6325981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:59:46+00:00 2026-05-24T16:59:46+00:00

I have this array: var arr = []; arr.push({name:k1, value:abc}); arr.push({name:k2, value:hi}); arr.push({name:k3, value:oa});

  • 0

I have this array:

var arr = [];
arr.push({name:"k1", value:"abc"});
arr.push({name:"k2", value:"hi"});
arr.push({name:"k3", value:"oa"});

is it possible to do get the value or a specific element by knowing the name ?

something like this:

arr['k2'].value

or

arr.get('k1')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:59:47+00:00Added an answer on May 24, 2026 at 4:59 pm

    Arrays are normally accessed via numeric indexes, so in your example arr[0] == {name:"k1", value:"abc"}. If you know that the name property of each object will be unique you can store them in an object instead of an array, as follows:

    var obj = {};
    obj["k1"] = "abc";
    obj["k2"] = "hi";
    obj["k3"] = "oa";
    
    alert(obj["k2"]); // displays "hi"
    

    If you actually want an array of objects like in your post you can loop through the array and return when you find an element with an object having the property you want:

    function findElement(arr, propName, propValue) {
      for (var i=0; i < arr.length; i++)
        if (arr[i][propName] == propValue)
          return arr[i];
    
      // will return undefined if not found; you could return a default instead
    }
    
    // Using the array from the question
    var x = findElement(arr, "name", "k2"); // x is {"name":"k2", "value":"hi"}
    alert(x["value"]); // displays "hi"
    
    var y = findElement(arr, "name", "k9"); // y is undefined
    alert(y["value"]); // error because y is undefined
    
    alert(findElement(arr, "name", "k2")["value"]); // displays "hi";
    
    alert(findElement(arr, "name", "zzz")["value"]); // gives an error because the function returned undefined which won't have a "value" property
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have for example this array-variable: var arr=[ [1,2,3] [4,5,6] ] How can
If I have an array like this: var arr = ['one','two','three']; I can access
I have this code: val arr: Array[Int] = ... val largestIndex = { var
Let's say I have this $(document).ready(function() { var array = $.makeArray($('p')); $(array).appendTo(document.body); }); });
I have an array like this: var arr1 = [a, b, c, d]; How
I have code similar to this filtering entries in an Array of Objects: var
I have an array created with this code: var widthRange = new Array(); widthRange[46]
I have an array of function callbacks, like this: class Blah { private var
Say, I have an array that looks like this: var playlist = [ {artist:Herbie
I have this PHP code - <?php for($i=1; $i<=1000; $i++) { $array=array(); $array[$i]=54*$i; $arr=array($array[$i].,);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.