2

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?

planet x
  • 1,509
  • 5
  • 26
  • 42

2 Answers2

6

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
thecodeparadox
  • 81,835
  • 21
  • 131
  • 160
  • Gracias por esta. One more to ask, what is the equivalent of php count() in JS? I tested that .length doesn't fit in here. – planet x May 07 '12 at 09:36
2

You use an object. And do it in much the same way.

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

Granted, that's not an array though.

Do take note that you can actually do this in Javascript arrays, and the interpreter will let you.

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

But you're not really pushing to the array, but rather extending the array object. You can check this out with this:

var foo = [];
foo['hello'] = 'world';

console.log(foo);            // []
console.log(foo['hello']);   // 'world'
console.log(foo.hello);      // 'world'
console.log(foo.length);     // 0

... so best to watch out for this gotcha. :D

Richard Neil Ilagan
  • 14,133
  • 5
  • 44
  • 64
  • 1
    you can remove the `$` now. it's not required in JS :D – Joseph May 07 '12 at 08:48
  • 1
    @Joseph ~ Yup, I know. But it adds to the dramatic, oh-my-gosh-so-similar effect! **dun dun duuuuun!** :D – Richard Neil Ilagan May 07 '12 at 08:52
  • @RichardNeilIlagan Gracias por esta. One more to ask, what is the equivalent of php `count()` in JS? I tested that `.length` doesn't fit in here. – planet x May 07 '12 at 09:36
  • For arrays, that just equates to `.length`. For objects, that's a different beast altogether though. The same question has been asked here, so best to check the solutions posted there. http://stackoverflow.com/questions/126100/how-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-javascrip – Richard Neil Ilagan May 07 '12 at 09:44