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

  • Home
  • SEARCH
  • 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 7913869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:56:36+00:00 2026-06-03T13:56:36+00:00

In my mind I want to push a key-value on array. Say for example

  • 0

In my mind I want to push a key-value on array.

Say for example in PHP this is done like this:

$err = array();
function check($k,$v) {
  $err[$k] = $v; 
}

How is this done exactly in JavaScript/jQuery?

  • 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-06-03T13:56:38+00:00Added an answer on June 3, 2026 at 1:56 pm

    Javascript key/value pair support in Object, not in Array

    Insert Element:

    var arr= [1,2,3],
        result = {};
    
    for(var key in arr) {
       result[key] = arr[key]; // in this case key will be 0,1,2 i.e index of array element
    }
    

    Check that a key exists:

    function checking(k, v) {
      if(result.k != null || result.k !=  undefined) {
        // do something
      }
    }
    

    According to your function:

    var err = {}; // not array, should be object
    function check(k,v) {
      err[k] = v; // or err.k = v;
    }
    

    More on insert:

    var x = [];
    
    x.some = 'some'; // OK; arrays are objects
    x['some'] = 'some'; // exactly the same
    

    Get value:

    x.home; // give 'some'
    x['home']; // give 'some'
    

    Count length:

    If you want to get length of an array you have to use .length.

    For example:

    x.length; // will return 1
    

    And if you want to count the length of an Object:

    var obj = {a: 'XYZ', b: 'PQR'};
    
    Object.keys(obj).length; // will return 2
    

    To Delete item:

    if you want to remove an item from an Object:

    delete obj[key];
    

    to delete an item from an array:

    delete arr[index]; // index is position of the element within array
    

    also to remove array element you can do

    arr.splice(0,2); // splice(index, howmany, [item1, item2, ...., itemx])
                     // in this example from start 2 items will delete
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say we have a document like this { _id : 1234, Data :
Hoping to get some clear advice on this one. I want to push updates
I don't mind so much about pirating etcetera, but I want to ensure that
In itext I have a chunk/phrase/paragraph (I dont mind which) and I want to
I’m coding an application with server resources in mind, so I don’t want to
This one boggles my mind. I've used this same script (different targets of course)
This is blowing my mind because it's probably an easy solution, but I can't
I would like to expose some data to client applications via HTTP. For example,
This has always bugged the hell out of me. Why are lists like this?
This is driving me nuts... I have an input string like so: String input

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.