How to insert multiple values to single key in javascript

krishna

I have an object array var codropsEvents={[date1]:['event1'],[date2]:['event2'}; I want to insert multiple values to event1 i.e {[date1]:['event1','event2'],..} I use the following code

<?php 
    $da=[];
    $qry="select * from events";
    $ex=mysqli_query($con,$qry);
    while($row=mysqli_fetch_assoc($ex))
    {
        $timestamp = strtotime($row['date']);
        $date= date('m-d-Y', $timestamp);
        $event=$row['event'];
        $da[]=$date;

        $eve[]=$event;


    }
    ?>
<script type="text/javascript">
        var a=<?php echo(json_encode($da)); ?>;
        var ev=  <?php echo(json_encode($eve)); ?>;
        var codropsEvents ={};
        for(var i=0;i<a.length;i++)
        {

            codropsEvents[a[i]] = '<span>'+[ev[i]]+'</span>';

        }

        </script>

But using this code I got something like this,

var codropsEvents={[date1]:[event1],[date1]:[event2]}

But I need all events with same date in a single key is {[date1]:['event1','event2',..],[date2]:[''event3','event4'..],..};

Please anyone can help me

Kamalakannan J

You need to check whether the key exists in the object, if not initialize it with empty array. Then push elements into the array.

<script type="text/javascript">
    var a=<?php echo(json_encode($da)); ?>;
    var ev=  <?php echo(json_encode($eve)); ?>;
    var codropsEvents ={};
    for(var i=0;i<a.length;i++)
    {

        if (!codropsEvents[a[i]]) {
            codropsEvents[a[i]] = [];
        }
        codropsEvents[a[i]].push('<span>'+[ev[i]]+'</span>');

    }

</script>   

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 to insert multiple of values in a single field in mysql

From Dev

How to insert multiple values in a single query

From Dev

Multiple values for single key

From Dev

Multiple values for single key

From Dev

JavaScript: Combine highest key values of multiple arrays into a single array

From Dev

how to insert multiple checkbox values in single column in database using php

From Dev

How transaction will work in single INSERT with multiple VALUES in SQL Server

From Dev

Multiple Values for a single key in a dictionary

From Dev

Transform multiple values to single key

From Dev

Multiple Values for a single key in a dictionary

From Dev

Performance Multiple inserts or multiple values single insert

From Dev

Performance Multiple inserts or multiple values single insert

From Dev

How to give multiple values to a single key using a dictionary?

From Dev

How to make Hadoop reducer output multiple values for a single key

From Dev

How to iterate thru multiple values in a single key in mongoose

From Dev

How to store multiple form data values to a single key in local storage

From Dev

insert multiple records at once for single foreign key

From Dev

Creating a single insert statement for multiple array values

From Dev

Insert and Update multiple values in single SQL Statement

From Dev

insert on duplicate key update with multiple values

From Dev

Insert multiple values and ignore duplicates on not key field

From Dev

Insert multiple values and ignore duplicates on not key field

From Dev

STL collection with multiple values for a single key

From Dev

Storing Multiple Values to a Single Key in NSDictionary

From Dev

Dictionary with single key multiple values in a list

From Dev

Adding multiple values into a single key in swift?

From Dev

Pandas Dictionary: How to return a key by matching an input value to multiple values assinged to a single key

From Dev

How to group multiple values in a key belonging to an object in Javascript?

From Dev

How can we add list or multiple values for a single key in web.config in asp .net

Related Related

  1. 1

    how to insert multiple of values in a single field in mysql

  2. 2

    How to insert multiple values in a single query

  3. 3

    Multiple values for single key

  4. 4

    Multiple values for single key

  5. 5

    JavaScript: Combine highest key values of multiple arrays into a single array

  6. 6

    how to insert multiple checkbox values in single column in database using php

  7. 7

    How transaction will work in single INSERT with multiple VALUES in SQL Server

  8. 8

    Multiple Values for a single key in a dictionary

  9. 9

    Transform multiple values to single key

  10. 10

    Multiple Values for a single key in a dictionary

  11. 11

    Performance Multiple inserts or multiple values single insert

  12. 12

    Performance Multiple inserts or multiple values single insert

  13. 13

    How to give multiple values to a single key using a dictionary?

  14. 14

    How to make Hadoop reducer output multiple values for a single key

  15. 15

    How to iterate thru multiple values in a single key in mongoose

  16. 16

    How to store multiple form data values to a single key in local storage

  17. 17

    insert multiple records at once for single foreign key

  18. 18

    Creating a single insert statement for multiple array values

  19. 19

    Insert and Update multiple values in single SQL Statement

  20. 20

    insert on duplicate key update with multiple values

  21. 21

    Insert multiple values and ignore duplicates on not key field

  22. 22

    Insert multiple values and ignore duplicates on not key field

  23. 23

    STL collection with multiple values for a single key

  24. 24

    Storing Multiple Values to a Single Key in NSDictionary

  25. 25

    Dictionary with single key multiple values in a list

  26. 26

    Adding multiple values into a single key in swift?

  27. 27

    Pandas Dictionary: How to return a key by matching an input value to multiple values assinged to a single key

  28. 28

    How to group multiple values in a key belonging to an object in Javascript?

  29. 29

    How can we add list or multiple values for a single key in web.config in asp .net

HotTag

Archive