How can I make dynamic associative array with jquery?

Darshana Rana

I want array like this:-

arr[0][from] = value
arr[0][to] = value

arr[1][from] = value
arr[1][to] = value

. . And so on.

I have input array html elements for from & to field.

<input type="text" name="from[]" value=""/>
<input type="text" name="to[]" value=""/>

Now, I am making array from below code and it's giving me error in console like "Uncaught TypeError: Cannot read property 'push' of undefined".

    var arr = [];
    $('input[name^=from]').each(function(index,val) {
              var from = $(this).val();
              if(typeof arr[index] === undefined)
                arr[index] = [];

             arr[index].push({from:from});  
    });
    $('input[name^=to]').each(function(index,val) {
              var to= $(this).val();
              if(typeof arr[index] === undefined)
                arr[index] = [];

             arr[index].push({to:to});  
    });

Even if I write arr[index]['from'] = from instead of arr[index].push({from:from}); then also giving error.

Please anybody help me to solve this issue. Thanks in advance.

Satpal

You need to push if object at index is not defined otherwise update it. You don't need to use typeof

var arr = [];
$('input[name^=from]').each(function(index,val) {
    var from = $(this).val();
    if (arr[index] === undefined) {
        arr[index] = {from: from};
    } else {
        arr[index].from = from;
    }
});
$('input[name^=to]').each(function(index,val) {
    var to= $(this).val();
    if (arr[index] === undefined) {
        arr[index] = {to: to};
    } else {
        arr[index].to = to;
    }
});

var arr = [];
$('input[name^=from]').each(function(index, val) {
  var from = $(this).val();
  if (arr[index] === undefined) {
    arr[index] = {
      from: from
    };
  } else {
    arr[index].from = from;
  }
});
$('input[name^=to]').each(function(index, val) {
  var to = $(this).val();
  if (arr[index] === undefined) {
    arr[index] = {
      to: to
    };
  } else {
    arr[index].to = to;
  }
});

console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="from[]" value="1" />
<input type="text" name="to[]" value="1" />
<input type="text" name="from[]" value="2" />
<input type="text" name="to[]" value="2" />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I print an associative array as a matrix

From Dev

Can I make the size of a byte array dynamic?

From Dev

How can I create an associative array from a foreach loop?

From Dev

How can I get the prev/next key from an associative array?

From Dev

How can I change the named keys in an associative array

From Dev

How can I make Meteor subscriptions dynamic?

From Dev

How i can make a dynamic css class?

From Dev

How can i make the for loop parameters dynamic

From Dev

How can i make a dynamic form in vuejs

From Dev

How can i make the for loop parameters dynamic

From Dev

How i can make a dynamic css class?

From Dev

How can I make a dynamic select box?

From Dev

How can I make actionListener method Dynamic

From Dev

WPF: How can I make the view dynamic?

From Dev

How can I make a dynamic value for update?

From Java

How to access array within associative array in jquery

From Dev

How can I make IF loop dynamic without adding condition for every element in the array?

From Dev

How do I write a swift array that can make the same UIImage appear for all my dynamic cells?

From Dev

how to make associative array using loop in javascript

From Dev

How to make associative array with number as string in Javascript

From Dev

dynamic associative array in d

From Dev

How can I copy associative array items to another single array item recursively?

From Dev

How can I build an associative array recursively using keys from another array?

From Dev

How can I make Symfony serialize empty associative arrays as empty JSON objects?

From Dev

How do I define an associative array?

From Dev

How to get the i'th element of an associative array?

From Dev

How can I make jquery script shorter?

From Dev

how can I make a bookmark that installs jquery?

From Dev

How can I make an array dynamically in php?

Related Related

  1. 1

    How can I print an associative array as a matrix

  2. 2

    Can I make the size of a byte array dynamic?

  3. 3

    How can I create an associative array from a foreach loop?

  4. 4

    How can I get the prev/next key from an associative array?

  5. 5

    How can I change the named keys in an associative array

  6. 6

    How can I make Meteor subscriptions dynamic?

  7. 7

    How i can make a dynamic css class?

  8. 8

    How can i make the for loop parameters dynamic

  9. 9

    How can i make a dynamic form in vuejs

  10. 10

    How can i make the for loop parameters dynamic

  11. 11

    How i can make a dynamic css class?

  12. 12

    How can I make a dynamic select box?

  13. 13

    How can I make actionListener method Dynamic

  14. 14

    WPF: How can I make the view dynamic?

  15. 15

    How can I make a dynamic value for update?

  16. 16

    How to access array within associative array in jquery

  17. 17

    How can I make IF loop dynamic without adding condition for every element in the array?

  18. 18

    How do I write a swift array that can make the same UIImage appear for all my dynamic cells?

  19. 19

    how to make associative array using loop in javascript

  20. 20

    How to make associative array with number as string in Javascript

  21. 21

    dynamic associative array in d

  22. 22

    How can I copy associative array items to another single array item recursively?

  23. 23

    How can I build an associative array recursively using keys from another array?

  24. 24

    How can I make Symfony serialize empty associative arrays as empty JSON objects?

  25. 25

    How do I define an associative array?

  26. 26

    How to get the i'th element of an associative array?

  27. 27

    How can I make jquery script shorter?

  28. 28

    how can I make a bookmark that installs jquery?

  29. 29

    How can I make an array dynamically in php?

HotTag

Archive