How do i push objects into the same array?

ii iml0sto1

I am having trouble applying objects to my array, every time i push the object into the array a new array is created.

I have made some edits at the bottom of the question.

I need an array of objects to look like this

var arr = [{name: 'bent', town: 'kansas'}, { name:'benny', town:'vegas'}];

Insted mine looks like this

var arr = [0:{name: 'bent', town: 'kansas'}1:{ name:'benny', town:'vegas'}]

PHP

$stmt = $dbCon->prepare("SELECT ctc_coins_name, "
        . " ctc_coins_town "
        . " FROM ctc_coins ");
$stmt->execute();
$result_coins = $stmt->fetchAll();

$coins = [];
foreach ($result_coins as $result) {
    $coins[] = [
        'name' => $result['ctc_coins_name'],
        'twon' => $result['ctc_coins_town]
    ];
}
echo json_encode($coins);

Javascript

function update_local_coins() {
    var http = new XMLHttpRequest();
    var url = "pages/ajax/getCoins.php";
    http.open("POST", url, true);
    http.onreadystatechange = function () {
        if (http.readyState === 4 && http.status === 200) {
            if (this.responseText) {
                jsonObj = JSON.parse(this.responseText);
                console.log(jsonObj);
                var coins = [];
                for (var l in jsonObj) {
                    coins.push(jsonObj[l]);
                }
                console.log("COINS");
                console.log(coins);
            } else {
                alert("Noobs");
            }
        }
    };
    http.send();
}

I have tried this with no luck

coins.concat(jsonObj[l]) 'returns empty array'
coins.push.apply(coins, jsonObj[l]) 'returns empty array'
coins.push(jsonObj[l]) 'returns the example array'

EDIT:

console.log(jsonObj);
(13) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0
:
{name: "Bitcoin", usd: "5622.73", btc: "1.00000000"}
1
:
{name: "Ethereum", usd: "338.81", btc: "0.06013390"}
2
:
{name: "Litecoin", usd: "58.65", btc: "0.01040990"}
3
:
{name: "Dash", usd: "307.31", btc: "0.05454360"}
4
:
{name: "Golem", usd: "0.21", btc: "0.00003759"}
5
:
{name: "NEM", usd: "0.21", btc: "0.00003674"}
6
:
{name: "Neutron", usd: "0.15", btc: "0.00002686"}
7
:
{name: "DigiByte", usd: "0.01", btc: "0.00000175"}
8
:
{name: "Siacoin", usd: "0.00", btc: "0.00000074"}
9
:
{name: "Linda", usd: "0.00", btc: "0.00000005"}
10
:
{name: "LiteDoge", usd: "0.00", btc: "0.00000001"}
11
:
{name: "ArcticCoin", usd: "0.05", btc: "0.00000922"}
12
:
{name: "NoLimitCoin", usd: "0.13", btc: "0.00002321"}
length
:
13
__proto__
:
Array(0)

EDIT: php echo json

[{"name":"Bitcoin","usd":"5622.73","btc":"1.00000000"},{"name":"Ethereum","usd":"338.81","btc":"0.06013390"},{"name":"Litecoin","usd":"58.65","btc":"0.01040990"},{"name":"Dash","usd":"307.31","btc":"0.05454360"},{"name":"Golem","usd":"0.21","btc":"0.00003759"},{"name":"NEM","usd":"0.21","btc":"0.00003674"},{"name":"Neutron","usd":"0.15","btc":"0.00002686"},{"name":"DigiByte","usd":"0.01","btc":"0.00000175"},{"name":"Siacoin","usd":"0.00","btc":"0.00000074"},{"name":"Linda","usd":"0.00","btc":"0.00000005"},{"name":"LiteDoge","usd":"0.00","btc":"0.00000001"},{"name":"ArcticCoin","usd":"0.05","btc":"0.00000922"},{"name":"NoLimitCoin","usd":"0.13","btc":"0.00002321"}]
kkaosninja

Your objective was to get the array of objects received in the AJAX response.

As I said in my first comment to your question, you already got this in this line

jsonObj = JSON.parse(this.responseText);

Now, you seem to think that you have received an object(with numbers being fields, and the objects being values). But this is just how console.log behaves :)

As for your final question regarding a way to confirm that is indeed an array, all you need to do is check the string representation of the result of JSON.parse() i.e use JSON.stringify. Below example demonstrates this.

EDIT: Have updated code snippet with demonstration of findMatches() posted in the comment.

        var s = '[{"name":"Bitcoin","usd":"5622.73","btc":"1.00000000"},{"name":"Ethereum","usd":"338.81","btc":"0.06013390"},{"name":"Litecoin","usd":"58.65","btc":"0.01040990"},{"name":"Dash","usd":"307.31","btc":"0.05454360"},{"name":"Golem","usd":"0.21","btc":"0.00003759"},{"name":"NEM","usd":"0.21","btc":"0.00003674"},{"name":"Neutron","usd":"0.15","btc":"0.00002686"},{"name":"DigiByte","usd":"0.01","btc":"0.00000175"},{"name":"Siacoin","usd":"0.00","btc":"0.00000074"},{"name":"Linda","usd":"0.00","btc":"0.00000005"},{"name":"LiteDoge","usd":"0.00","btc":"0.00000001"},{"name":"ArcticCoin","usd":"0.05","btc":"0.00000922"},{"name":"NoLimitCoin","usd":"0.13","btc":"0.00002321"}]';
    
        var arrayOfObjects = JSON.parse(s);
    
        var stringifiedArray = JSON.stringify(arrayOfObjects);
    
        document.getElementById("op").innerText = stringifiedArray;
    
        function findMatches(arr, searchString) {
            const matches = [];
            arr.forEach(obj => {
                for(const key in obj) {
                if (obj[key].includes(searchString)) {
                    matches.push(obj[key]);
                }
            }
        });
            return matches;
        }
    
    var matches = findMatches(arrayOfObjects, "Lite");
    
    document.getElementById("mr").innerText = JSON.stringify(matches);
<p id="op">

    <!-- Result of JSON.stringify will appear here -->

    </p>
    <hr>
    <p id="mr">
    <!-- Result of findMatches will appear here -->
    </p>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How do I save objects that I push into an array, then display them using local storage?

分類Dev

How do i filter an array inside of a array of objects?

分類Dev

How do I push data from a Subject observable to an array in the component?

分類Dev

How do I create an boolean equals method compares objects in an array

分類Dev

How do I convert array of objects to object in javascript?

分類Dev

How do I create a react list component from an array of objects?

分類Dev

How do I display array objects using a simple loop and 'if' statements?

分類Dev

How do I format an array of objects by iteration in Model in Ruby on Rails

分類Dev

how do i sort an array of objects via the comparable interface?

分類Dev

How do I assign the same array values to 2 different variables

分類Dev

How do i add an Object to end of an array in javascript. array.push seams not working for object here

分類Dev

How to merge Array of Objects based on the same value?

分類Dev

How do I map over a contacts array to produce a new array with objects and properties?

分類Dev

How can I push data into an array?

分類Dev

How do I deploy REST API using an imported array of JS objects?

分類Dev

How do I loop over an array of objects to return a property dependent on a corresponding property within that object?

分類Dev

How do I create an array of objects from a class, and then pass those to subclasses?

分類Dev

How do I sort an array filled with objects, based on two values of the object?

分類Dev

'push' new number into an array of objects

分類Dev

push objects into array in sequential order

分類Dev

How can I display an array of objects?

分類Dev

How do I get TypeScript Array.map() to return the same as VanillaJS?

分類Dev

How do I display a list of objects in an Exception?

分類Dev

How do I create a vector of objects in R?

分類Dev

How do I retrieve data independently from nested JSON objects that are part of the same group in android/java? ( more clarity/details in body)

分類Dev

How do you merge two different arrays of objects of the same size?

分類Dev

How can I push the elements into array in a desired format

分類Dev

How can I push regex matches to array in java?

分類Dev

How do I determine branch name or id in webhook push event?

Related 関連記事

  1. 1

    How do I save objects that I push into an array, then display them using local storage?

  2. 2

    How do i filter an array inside of a array of objects?

  3. 3

    How do I push data from a Subject observable to an array in the component?

  4. 4

    How do I create an boolean equals method compares objects in an array

  5. 5

    How do I convert array of objects to object in javascript?

  6. 6

    How do I create a react list component from an array of objects?

  7. 7

    How do I display array objects using a simple loop and 'if' statements?

  8. 8

    How do I format an array of objects by iteration in Model in Ruby on Rails

  9. 9

    how do i sort an array of objects via the comparable interface?

  10. 10

    How do I assign the same array values to 2 different variables

  11. 11

    How do i add an Object to end of an array in javascript. array.push seams not working for object here

  12. 12

    How to merge Array of Objects based on the same value?

  13. 13

    How do I map over a contacts array to produce a new array with objects and properties?

  14. 14

    How can I push data into an array?

  15. 15

    How do I deploy REST API using an imported array of JS objects?

  16. 16

    How do I loop over an array of objects to return a property dependent on a corresponding property within that object?

  17. 17

    How do I create an array of objects from a class, and then pass those to subclasses?

  18. 18

    How do I sort an array filled with objects, based on two values of the object?

  19. 19

    'push' new number into an array of objects

  20. 20

    push objects into array in sequential order

  21. 21

    How can I display an array of objects?

  22. 22

    How do I get TypeScript Array.map() to return the same as VanillaJS?

  23. 23

    How do I display a list of objects in an Exception?

  24. 24

    How do I create a vector of objects in R?

  25. 25

    How do I retrieve data independently from nested JSON objects that are part of the same group in android/java? ( more clarity/details in body)

  26. 26

    How do you merge two different arrays of objects of the same size?

  27. 27

    How can I push the elements into array in a desired format

  28. 28

    How can I push regex matches to array in java?

  29. 29

    How do I determine branch name or id in webhook push event?

ホットタグ

アーカイブ