2

I am trying to Push key-value pair in an Array in a loop.

var arr_ReservationType = new Array();
$("#table tr").each(function()
{
    arr_ReservationType=
  { 
    key: "value"
  }
});

I have also tried

arr_ReservationType[index].push({key:"value"});

and

 arr_ReservationType[index].push({key:"value"});
    index++;
 });

But all these codes are adding an object to the array, not the key-value pair.

I am not able to find the exact solution to my problem. Please suggest if there is a similar solution available or any alternate solution. Thanks.

1
  • Array ( [0] => Array ( [ReservationTypeName] => 123 ) [1] => Array ( [ReservationTypeName] => 123 ) ) Commented Aug 27, 2018 at 11:57

3 Answers 3

1

In javascript You can't directly push key:value pair in array like other languages. U need to push objects in an array with key:value pair like this arr_ReservationType.push({key:"value"});.

It's possible only through array of objects.

Sign up to request clarification or add additional context in comments.

2 Comments

then to retrieve that in php , i need to loop in nested array , is there any alternate ?
Yeah...U can do it. If it nested then again u have to create an array inside array keep pushing objects..
1

You just need to do arr_ReservationType.push({key:"value"}); as arr_ReservationType is your array and it will add the value in the array.

You code will be something like this:

$('#table tr').each(function () {
  arr_ReservationType.push({
    key: 'value'
  });
});

1 Comment

showing Array ( [0] => Array ( [ReservationTypeName] => 123 ) [1] => Array ( [ReservationTypeName] => 123 ) )
0

you need to push your data in the array as

var arr_ReservationType = new Array();
$("#table tr").each(function(){
    arr_ReservationType.push({key:"value"});
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.