如何将对象推入同一个数组?

时间:2017-10-14 13:16:48

标签: javascript arrays object push

我在将对象应用到数组时遇到问题,每次将对象推入数组时都会创建一个新数组。

我在问题的底部做了一些修改。

我需要一个像这样的对象数组

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

Insted mine看起来像这样

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();
}

我试过这个没有运气

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

编辑:

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)
编辑: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"}]

1 个答案:

答案 0 :(得分:1)

您的目标是获取AJAX响应中收到的对象数组。

正如我在你对你的问题的第一次评论中所说,你已经在这一行中得到了这个

jsonObj = JSON.parse(this.responseText);

现在,您似乎认为您已收到一个对象(数字为字段,对象为值)。但这就是console.log的行为方式:)

关于确认确实是数组的方法的最后一个问题,您需要做的就是检查JSON.parse()结果的字符串表示,即使用JSON.stringify。下面的例子说明了这一点。

编辑:更新了代码段,并在评论中发布了findMatches()演示文稿。



        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>
&#13;
&#13;
&#13;